void josnscan()
{
char *str = "{\"test\":\"0100\", \"test2\":\"0200#2\",\"test3\":123,\"test4\":[\"0100\", \"0200#2\"]}";
cJSON *root;
root = cJSON_Parse(str);
cJSON *childNode;
int arraySize = cJSON_GetArraySize(root);
printf("arraySize %d\n", arraySize);
int i;
for(i = 0; i < arraySize; i++)
{
childNode = cJSON_GetArrayItem(root, i);
if(childNode == NULL) continue;
printf("%d %s\n", childNode->type, childNode->string);
if(childNode->type == cJSON_String)
{
printf("childNode %s\n", childNode->valuestring);
}
else if(childNode->type == cJSON_Number)
{
printf("childNode %d\n", childNode->valueint);
}
else if(childNode->type == cJSON_Array)
{
cJSON *tmp;
int Size = cJSON_GetArraySize(childNode);
int j;
for(j = 0; j < Size; j++)
{
tmp = cJSON_GetArrayItem(childNode, j);
if(tmp == NULL) continue;
printf("tmp %s\n", tmp->valuestring);
}
}
}
}
运行结果
arraySize 4
16 test
childNode 0100
16 test2
childNode 0200#2
8 test3
childNode 123
32 test4
tmp 0100
tmp 0200#2