[英]parse json arrays using cJSON library
首先,这是一个非常广泛的问题,在我要求社区为我编写代码时可能会遇到。 那不是我的意图,但是我很迷茫,我不知道如何提供足够的信息。
我尝试使用Dave Gamble编写的cJSON库,我发现这对于将嵌入式设备用于JSON解析和组合非常有用。
读取以下JSON数组
{
"name": "Jack",
"types":[23,56,78],
"format": {
"type": "rect",
"width": 1920, }
}
..并解析使用此方法获得的对象
cJSON *format = cJSON_GetObjectItem(json,"format");
int framerate = cJSON_GetObjectItem(format,"width")->valueint;
但我无法解析键“名称”和对象简单键值,
我试过了
cJSON *array = cJSON_GetArrayItem(json,"types");
int value = cJSON_GetArrayItem(format1,1)->valueint;
但是没有用,如何解析数组对象和简单键值。
我认为JSON元素应遵循key:value格式。
{
"name": "Jack",
"types":[{"type" : 23}, {"type" : 56}, {"type":78}],
"format": {
"type": "rect",
"width": 1920, }
}
您的json很好。 您可以遍历cJSON中的值数组:
cJSON * array = cJSON_GetObjectItem(json, "types");
for (i = 0 ; i < cJSON_GetArraySize(array) ; i++)
{
printf("%d ",cJSON_GetArrayItem(array, i)->valueint);
}
将打印
23 56 78
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.