json_object_is_type: Check the type of json object

Posted by Joys of Programming on in C/C++, JSON, json-c

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_null\n");
    }
    if (json_object_is_type(val, json_type_boolean)) {
     printf("json_type_boolean\n");
    }
    if (json_object_is_type(val, json_type_double)) {
     printf("json_type_double\n");
    }
    if (json_object_is_type(val, json_type_int)) {
     printf("json_type_int\n");
    }
    if (json_object_is_type(val, json_type_object)) {
     printf("json_type_object\n");
    }
    if (json_object_is_type(val, json_type_array)) {
     printf("json_type_array\n");
    }
    if (json_object_is_type(val, json_type_string)) {
     printf("json_type_string\n");
    }
  }
}

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
    }
}


Tags: , , ,

Comments:

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Copyright © 2009-2012 Joys of Programming All rights reserved.
Desk Mess Mirrored v1.8.1 theme from BuyNowShop.com.