cJSON获取json数据的所有的key
cJSON
Ultralightweight JSON parser in ANSI C
项目地址:https://gitcode.com/gh_mirrors/cj/cJSON
免费下载资源
·
小记
使用cJSON实现获取json数据中所有的key的值,然后对key值做相应的处理操作
笔记
首先先看了一下cJSON的结构体,双向链表结构。
// cJSON结构体:
typedef struct cJSON {
struct cJSON *next,*prev; // next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem
struct cJSON *child; // An array or object item will have a child pointer pointing to a chain of the items in the array/object.
int type; // The type of the item, as above.
char *valuestring; // The item's string, if type==cJSON_String
int valueint; // The item's number, if type==cJSON_Number
double valuedouble; // The item's number, if type==cJSON_Number
char *string; // The item's name string, if this item is the child of, or is in the list of subitems of an object.
} cJSON;
包含了key以及指向下一个结点的指针
那么就直接遍历链表就好了
实现如下:
#include <stdio.h>
#include <string.h>
#include "cJSON.h"
#include "pub_function.h"
int main(){
cJSON * test_json;
char *content=read_json("./testjson.json");
test_json=cJSON_Parse(content);
cJSON * testitem_json=cJSON_GetObjectItem(test_json,"device_type");
while (testitem_json->next!=NULL)
{
printf("%s\n",testitem_json->string);
testitem_json=testitem_json->next;
}
}
运行结果:
json文件:
{
"device_type": "WSD_General",
"modal_type": "WSD",
"device_name": "SNMP网络路由",
"address": "192.168.32.2",
"get_community": "public",
"set_community": "public"
}
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 个月前
更多推荐
已为社区贡献3条内容
所有评论(0)