json_object_get_type: Get the type of JSON object

A json object can have any of the following types

  1. null,
  2. boolean,
  3. double,
  4. int,
  5. object,
  6. array,
  7. string,

If you are working with JSON-C, you may need to know the type of every JSON object you are parsing. json_object_get_type() gives you the type of every object.

The syntax of json_object_get_type()

enum json_type json_object_get_type(struct json_object *this);

It accepts a json_object and returns the type of the json object. The values can be

json_type_boolean,
json_type_double,
json_type_int,
json_type_object,
json_type_array,
json_type_string,

The following program demonstrates this

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

int main() {
char * string = "{"name" : "programming"}";
json_object * jobj = json_tokener_parse(string);
enum json_type type = json_object_get_type(jobj);
printf("type: ",type);
switch (type) {
case json_type_null: printf("json_type_nulln");
break;
case json_type_boolean: printf("json_type_booleann");
break;
case json_type_double: printf("json_type_doublen");
break;
case json_type_int: printf("json_type_intn");
break;
case json_type_object: printf("json_type_objectn");
break;
case json_type_array: printf("json_type_arrayn");
break;
case json_type_string: printf("json_type_stringn");
break;
}
}

Let’s compile and execute the program.

$ ./a.out
type: json_type_object
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