json_object_is_type: Check the type of json object

json_object_is_type is used to check whether the type of the json object is same as that of the specified type. So you can easily guess that the function takes two arguments


#include <json/json.h>
#include <stdio.h>

int main() {
  char * string = "{"sitename" : "joys of programming",
                     "tags" : [ "c" , "c++", "java", "PHP" ],
                     "author-details": { "name" : "Joys of Programming", "Number of Posts" : 10 } 
                     }";
  json_object * jobj = json_tokener_parse(string);
  enum json_type type;
  json_object_object_foreach(jobj, key, val) {
    printf("type: ",type);
    if (json_object_is_type(val, json_type_null)) {
     printf("json_type_nulln");
    }
    if (json_object_is_type(val, json_type_boolean)) {
     printf("json_type_booleann");
    }
    if (json_object_is_type(val, json_type_double)) {
     printf("json_type_doublen");
    }
    if (json_object_is_type(val, json_type_int)) {
     printf("json_type_intn");
    }
    if (json_object_is_type(val, json_type_object)) {
     printf("json_type_objectn");
    }
    if (json_object_is_type(val, json_type_array)) {
     printf("json_type_arrayn");
    }
    if (json_object_is_type(val, json_type_string)) {
     printf("json_type_stringn");
    }
  }
}

The output of the program is something like this

type: json_type_string
type: json_type_array
type: json_type_object

The input to the program is

{
    "sitename" : "joys of programming",
    "tags" : [
        "c" ,
        "c++",
        "java",
        "PHP"
    ],
    "author-details": {
        "name" : "Joys of Programming",
        "Number of Posts" : 10
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s