data rate

stay here and get updated with technology

Understanding basics

Part3:
Prev | Next | Index

Data types

As mentioned in earlier the json supports all data types which are enumerated as

typedef enum json_type {
json_type_null,
json_type_boolean,
json_type_double,
json_type_int,
json_type_object,
json_type_array,
json_type_string,
} json_type;

this is just for understanding the json_types,will explain shortly

Json API’s

Which are the functions used to handle the json.There are lot of API’s available in the json-c lib,few important ones among them are

1.json_object_new_object()    — used to declare object

extern struct json_object* json_object_new_object(void);

which returns a pointer to a json object
eg :

json_object *  myJsonObject = json_object_new_object();

2.json_object_new_json_boolean()     –creates a new json_object type of boolean

extern struct json_object* json_object_new_boolean(json_bool b);

eg :

  //creating a json object
  json_object * myObj = json_object_new_object();
  //creating a json boolean
  json_object *myBool = json_object_new_boolean(1);
  //creating json object
  json_object_object_add(myObj,"Linux", myBool);

ie; myobj =Linux :TRUE

 

2.json_tokener_parse()    —parse a string to  json object

extern struct json_object* json_tokener_parse(const char *str);

eg :

char *message="{ 'Name':'linux' , 'version':14 , 'OS':True }";                       myJsonObject = json_tokener_parse(msg);//parse the string data to json object

now the json object myJsonObject is = Name:linux , version:14 ,OS :TRUE

3.json_object_to_json_string()    –convert a json object to

%d bloggers like this: