Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using cJSON to read in a JSON array

Tags:

json

c

cjson

I am attempting to use the cJSON library, written by Dave Gamble, to read in the following JSON array:

"items": 
[
    {
        "name": "command",
        "index": "X",
        "optional": "0"
    },
    {
        "name": "status",
        "index": "X",
        "optional": "0"
    }
]

From reading his documentation, I found ways to read in individual Objects, but nothing regarding Arrays, and I wasn't able to surmise how to do it from the examples given.

Here's what I'm trying:

cJSON* request_json = NULL;
cJSON* items = cJSON_CreateArray();
cJSON* name = NULL;
cJSON* index = NULL;
cJSON* optional = NULL;

request_json = cJSON_Parse(request_body);

items = cJSON_GetObjectItem(request_json, "items");

name = cJSON_GetObjectItem(items, "name");
index = cJSON_GetObjectItem(items, "index");
optional = cJSON_GetObjectItem(items, "optional");

I know this is wrong, and not just because it's not working, but I can't figure out how to make it right.

Obviously I'm going to need to loop the process of reading in all of the entries for each index of the array. I have no idea how I'm going to do that though, because I don't know where I should be using the indexes in this code, or if it is even the right start. There is a cJSON_GetArrayItem(), but it takes only a number (presumably an index) and no string to indicate which field it wants.

like image 457
Nealon Avatar asked Jun 03 '13 15:06

Nealon


People also ask

How do you parse an array in JSON?

Use the JSON. parse() method to pase a JSON array, e.g. JSON. parse(arr) . The method parses a JSON string and returns its JavaScript value or object equivalent.

Does JSON parse work on arrays?

JSON. parse() converts array data into a Javascript array. The array data must be a valid JSON string.

What is cJSON in C?

cJSON aims to be the dumbest possible parser that you can get your job done with. It's a single file of C, and a single header file. JSON is described best here: http://www.json.org/ It's like XML, but fat-free. You use it to move data around, store things, or just generally represent your program's state.

Is array of strings a valid JSON?

In JSON, array values must be of type string, number, object, array, boolean or null. In JavaScript, array values can be all of the above, plus any other valid JavaScript expression, including functions, dates, and undefined.


3 Answers

Document mentions about parse_object().

I think this is what you need to do.

void parse_object(cJSON *root)
{
  cJSON* name = NULL;
  cJSON* index = NULL;
  cJSON* optional = NULL;

  int i;

  cJSON *item = cJSON_GetObjectItem(items,"items");
  for (i = 0 ; i < cJSON_GetArraySize(item) ; i++)
  {
     cJSON * subitem = cJSON_GetArrayItem(item, i);
     name = cJSON_GetObjectItem(subitem, "name");
     index = cJSON_GetObjectItem(subitem, "index");
     optional = cJSON_GetObjectItem(subitem, "optional"); 
  }
}

Call this function as

request_json = cJSON_Parse(request_body);
parse_object(request_json);
like image 148
Denny Mathew Avatar answered Nov 10 '22 03:11

Denny Mathew


If you want to run slightly faster, this is what the code looks like:

void parse_array(cJSON *array)
{
  cJSON *item = array ? array->child : 0;
  while (item)
  {
     cJSON *name = cJSON_GetObjectItem(item, "name");
     cJSON *index = cJSON_GetObjectItem(item, "index");
     cJSON *optional = cJSON_GetObjectItem(item, "optional"); 

     item=item->next;
  }
}

This avoids the O(n^2) cost that RBerteig correctly points out.

Call with:

parse_array(cJSON_GetObjectItem(cJSON_Parse(request_body),"items"));
like image 23
Dave Gamble Avatar answered Nov 10 '22 02:11

Dave Gamble


IMHO, this is one example of a case where you should burst the library's encapsulation and work directly with it's object data structure. cJSON.h defines the core object as the following struct:

/* The cJSON structure: */
typedef struct cJSON {
    struct cJSON *next,*prev;   /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
    struct cJSON *child;        /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */

    int type;                   /* The type of the item, as above. */

    char *valuestring;          /* The item's string, if type==cJSON_String */
    int valueint;               /* The item's number, if type==cJSON_Number */
    double valuedouble;         /* The item's number, if type==cJSON_Number */

    char *string;               /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
} cJSON;

(One could quibble with some of the naming choices the author made, of course. But good naming is hard.)

The key thing to note is that both JSON Objects and JSON Arrays have a non-null child field, which points to a doubly-linked list of their children. Children of JSON Objects also have non-null string fields which contains the field name associated with that child.

So, to generically iterate over the JSON Array ja in O(n) time, calling a function for each element, you write something like this:

cJSON_ForEachItem(cJSON *ja, int (*f)(cJSON *ja, int i, cJSON *jchild)) 
{
    cJSON *jchild;
    int i;
    for (jchild=ja->child, i=0; jchild; jchild=jchild->next, ++i) {
        // do something here with the ith child...
        if (f(ja, i, jchild))
            break;
    }
}

Since Objects and Arrays only differ internally in the presence of names for each child item, that function will also iterate the fields of an object. The callback can tell because ja->type will be either cJSON_Array or cJSON_Object, and jchild->string will be non-null for Objects as well.

Doing the same iteration by calling cJSON_GetArraySize() and using cJSON_GetArrayItem() will be order O(n^2) because it has to traverse the linked list each time to locate the nth item.

Arguably, cJSON should include some generic ForEach functions, but that might represent the beginning of a significant amount of scope-creep away from it's professed original goal of being "the dumbest possible parser that you can get your job done with".

like image 4
RBerteig Avatar answered Nov 10 '22 01:11

RBerteig