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