0

I am using cJSON to parse a string containing key-values. I want to generate my structure dynamically and for that I need to read all the keys from this string.

e.g. I have a json like below and I want to read all the keys at run-time. I don't know which all keys would be present in the json.

{
    "name": "abc",
    "class": "First",
    "division": "A",
    "age": "10"
}

How can I read keys and values without actually knowing the keys ?

I tried using pointers to link to next child, but that does not seem to give me right values.

cJSON *root = cJSON_Parse(strMyJson);
cJSON *temp = root;

std::cout << "----------" << temp->child->string << "\n";//displays key - correct
std::cout << "----------" << temp->child->valuestring << "\n"; //displays value - correct

//below starts causing problem
temp = temp->child->next;

while (temp != NULL)
{
    std::cout << "----------" << temp->string << "\n";
    std::cout << "----------" << temp->valuestring << "\n";
    temp = temp->child->next;
}

Appreciate your help !

-Thanks, S

Shaila
  • 51
  • 7

2 Answers2

0

Resolved issue Not sure why, but I need to handle root case separately.

Below code worked !

        cJSON *root = cJSON_Parse(strMyJson);
        if(NULL == root)
        {
            std::cout << __func__ << " invalid JSON\n";
            return false;
        }

        cJSON *temp = root;

        temp = temp->child->next;

        std::cout << "value: " << temp->valuestring << "\t";
        std::cout << "key : " << temp->string << "\n";

        temp = temp->next;

        while (temp != NULL)
        {

            std::cout << "----------" << temp->string << "\n";
            std::cout << "----------" << temp->valuestring << "\n";

            temp = temp->next;
        }

-Thanks, S

Shaila
  • 51
  • 7
0

the structure of the root JSON is like this:

{ -----------------------------> root  : cJSON_Object 
    "name": "abc",-------------> child : cJSON_String
    "class": "First",----------> child->next : cJSON_String
    "division": "A",-----------> child->next->next : cJSON_String
    "age": "10"----------------> child->next->next->next : cJSON_String
}

blow snippet will help you understand how cJSON works :)

int main(void)
{
  cJSON *root = cJSON_Parse(jsonstring);
  cJSON *temp = root;
  printf("root item's type--- %d\n", temp->type);      //root item's type is cJSON_Object


  printf("type--- %d\n", temp->child->type);      // cJSON type: 16 cJSON_String; 64 cJSON_Object
  printf("string--- %s\n", temp->child->string);      //displays key - correct
  printf("string--- %s\n", temp->child->valuestring);; //displays value - correct

  temp = temp->child->next;
  char *tempstr = cJSON_Print(temp);
  printf("tempstr = %s\n", tempstr);
  while (temp != NULL)
  {
    printf("type--- %d\n", temp->type);      //displays type - correct
    printf("string--- %s\n", temp->string);      //displays key - correct
    printf("string--- %s\n", temp->valuestring); //displays value - correct
    temp = temp->next;
  }
}
Alan Wang
  • 41
  • 5