Basic tutorial on JSON-GLib for Tizen

Introduction:

Tizen natively supports JSON-GLib (version 0.10.4). Before going further I think it will be helpful for all to just skim over the JSON-GLib API at JSON-GLib official webpage.

Objective:

In this tutorial I will take a json file from Tizen devices directory and parse it. You can download the test json file from hereIf we visualize the structure of the json file it will look like below:

test.json Top View

test.json Top View

The JSON node has 1 object member (“data”) and 1 integer member (“total”). The “data” object further consists more objects with different names such as “c_466792”, “c_466783” etc. [Note: we are getting this json file from a remote server, these object names and object count will change frequently.] If we expand one of these object member we will get below structure:

Object Expanded View

Object Expanded View

Our target is to read “title” shown in red rectangle above on each object and print it in Tizen Debug Log.

Coding:

We will read our json file from internal sdcard. So we need sdcard reading permission for our app. In tizen-manifest.xml file please add following codes:


 <privilege>http://tizen.org/privilege/mediastorage</privilege>
 <privilege>http://tizen.org/privilege/externalstorage.appdata</privilege>
 <privilege>http://tizen.org/privilege/externalstorage</privilege>

Add following header files:


#include <json-glib.h>
#include <glib-object.h>

The following program will read the json file stored in “/opt/usr/media/Downloads/test.json” and print the titles in Debug Log.


// Declare necessary Variables
 // We will use JsonParser instead of JsonReader for json parsing.
 JsonParser *parser;
 JsonNode *root;
 GError *error;

 error = NULL;

 // Create a new json parser
 parser = json_parser_new();

 // Load from file


 gboolean result = json_parser_load_from_file(parser, "/opt/usr/media/Downloads/test.json", &error);
 //gboolean result = json_parser_load_from_data(parser, s.ptr, strlen(s.ptr), NULL );


 // If result is TRUE then the file was loaded and parsed Successfully.

 if(result){
     root = json_parser_get_root(parser);

     // A new JsonObject variable for loading root object of the json.
     JsonObject *object;

     // Get All the objects in the Node. In this case 2 Object ["data" + "total"].
     object = json_node_get_object(root);

     // "data" Objects
     JsonObject *object_member = json_object_get_object_member(object, "data");

     // Print Size of the "data", in this case it will be 20.
     dlog_print(DLOG_DEBUG, "SRBD", "Size: %d", json_object_get_size(object_member));


     // This list will contain all the names of objects under "data"
     GList *my_list;
     my_list = json_object_get_members(object_member);


     for (; my_list != NULL; my_list = my_list->next){
         // Here we get each Objects Name in the my_list->data

         // Load each object such as c_466782, c_466783 etc. in a new variable.
         JsonObject *each_object = json_object_get_object_member(object_member, my_list->data);

         // Prints Each Objects Titles
         dlog_print(DLOG_DEBUG, "SRBD" ,"TEST: %s ", json_object_get_string_member(each_object, "title"));


     }

     // Parsing Successful
     dlog_print(DLOG_DEBUG, "SRBD" ,"SUCCESS on PARSing");

 } else{

     // We Failed!
     dlog_print(DLOG_DEBUG, "SRBD" ,"FAILED to PARSE");
 }

Output:

Our output will look like below in the Tizen Log.

Output

Output

Leave a comment