首先,这是一个非常广泛的问题,当我要求社区为我编写代码时,可能会遇到这个问题。这不是我的本意,但我很迷茫,我不知道如何提供足够的信息。
我正在尝试使用由 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 没问题。您可以遍历 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
关于c - 使用 cJSON 库解析 json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23369799/