Showing posts with label json. Show all posts
Showing posts with label json. Show all posts

Friday, September 29, 2017

handle JSON with jansson in C


int main()
{
    printf("Hello jansson...\n");

    const char * data_str = "{\
              \"operation\":\"mark-tier-movement\",\
              \"mark_tier_movement_request\":{\
                  \"mark_tier_movement\":true\
              }\
      }";

    json_t *root;
    json_error_t error;
    root = json_loads(data_str, 0, &error);
    if (!root) {
        printf("error: on line %d: %s\n", error.line, error.text);
        return 1;
    }

    json_t *operation;
    const char *msg_txt;
    operation = json_object_get(root, "operation");
    msg_txt = json_string_value(operation);
    printf("operation is: %s\n", msg_txt);

    return 0;
}

[root@localhost dev]# ./demo
Hello jansson...
operation code: is: mark-tier-movement

Thursday, January 12, 2017

JSON in Java

Add to your Maven dependency
1
2
3
4
5
<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.json</artifactId>
    <version>1.0.4</version>
</dependency>
Demo:
1
2
3
4
5
6
7
String json = "{\"id\":3}";
JsonReader reader = Json.createReader(new StringReader(json));
JsonObject rootObject = reader.readObject();
reader.close();

int devID = rootObject.getInt("id");
String sDevID = Integer.toString(devID);