Search code examples
ccjson

How to parse the following object in cJSON in generic way


As i am new to the cJOSN parser, someone please help to parse the following json in a generic way.

Here name and Strength is fixed. So if i choose person1, have to get person1's name and strength key values.(Number of persons may increase or decrease)

{
  "details": [
    {
      "person1": [
        {
          "name": "xxx",
          "strength": "abc"
        }
      ],
      "person2": [
        {
          "name": "yyy",
          "strength": "def"

        }
      ],
      "person3": [
        {
          "name": "zzz",
          "strength": "abc"

        }
      ]
    }
  ]
}

Solution

  • The documentation seems to be a good place to start. Here's a minimally reproducible answer:

    #include <stdio.h>
    #include "cJSON.h"
    
    const char *myJsonString = "{" \
    "  \"details\": [ " \
    "    { " \
    "      \"person1\": [ " \
    "        { " \
    "          \"name\": \"xxx\", " \
    "          \"strength\": \"abc\" " \
    "        } " \
    "      ], " \
    "      \"person2\": [ " \
    "        { " \
    "          \"name\": \"yyy\", " \
    "          \"strength\": \"def\" " \
    "        } " \
    "      ], " \
    "      \"person3\": [ " \
    "        { " \
    "          \"name\": \"zzz\", " \
    "          \"strength\": \"abc\" " \
    "        } " \
    "      ] " \
    "    } " \
    "  ] " \
    "}";
    
    int main()
    {
        cJSON *root = cJSON_Parse(myJsonString);
        cJSON *details = cJSON_GetObjectItem(root, "details");
        int detailsIndex;
        for (detailsIndex = 0; detailsIndex < cJSON_GetArraySize(details); detailsIndex++) {
            cJSON *people = cJSON_GetArrayItem(details, detailsIndex);
            int personIndex;
            for (personIndex = 0; personIndex < cJSON_GetArraySize(people); personIndex++) {
                cJSON *personItems = cJSON_GetArrayItem(people, personIndex);
                const char *personKeyName = personItems->string;
                int itemIndex;
                for (itemIndex = 0; itemIndex < cJSON_GetArraySize(personItems); itemIndex++) {
                    cJSON *personItem = cJSON_GetArrayItem(personItems, itemIndex);
                    const char *name =  cJSON_GetObjectItem(personItem, "name")->valuestring;
                    const char *strength = cJSON_GetObjectItem(personItem, "strength")->valuestring;
                    fprintf(stderr, "personKeyName [%s] name [%s] strength [%s]\n", personKeyName, name, strength);
                }
            }
        }
        cJSON_Delete(root);
        return 0;
    }
    

    To compile:

    $ wget -qO- https://raw.githubusercontent.com/insertion/cJOSN/master/cJSON.c > cJSON.c
    $ wget -qO- https://raw.githubusercontent.com/insertion/cJOSN/master/cJSON.h > cJSON.h
    $ gcc -Wall cJSON.c test.c -o test -lm
    

    To run:

    $ ./test
    personKeyName [person1] name [xxx] strength [abc]
    personKeyName [person2] name [yyy] strength [def]
    personKeyName [person3] name [zzz] strength [abc]