cJSON 如何遍历所有对象,获取到未知键名的内容
cJSON
Ultralightweight JSON parser in ANSI C
项目地址:https://gitcode.com/gh_mirrors/cj/cJSON
免费下载资源
·
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
GitHub 加速计划 / cj / cJSON
10.45 K
3.16 K
下载
Ultralightweight JSON parser in ANSI C
最近提交(Master分支:2 个月前 )
424ce4ce
This reverts commit 5b502cdbfb21fbe5f6cf9ffbd2b96e4281a741e6.
Related to #860
4 个月前
32497300 - 5 个月前
更多推荐
已为社区贡献7条内容
所有评论(0)