cJSON获取json数据的所有的key

小记

使用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"
}
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要使用CJSON获取JSON数据中的,您可以使用CJSON提供的函数来访问和提取JSON对象中的对。下面是一个示例代码,演示了何使用CJSON库来获取JSON数据中定: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include "cJSON.h" int main() { const char* json_data = "{\"key1\": \"value1\", \"key2\": 42, \"key3\": [1, 2, 3]}"; cJSON* root = cJSON_Parse(json_data); if (root == NULL) { printf("Failed to parse JSON data!\n"); return 1; } cJSON* value1 = cJSON_GetObjectItemCaseSensitive(root, "key1"); if (cJSON_IsString(value1) && (value1->valuestring != NULL)) { printf("Value of key1: %s\n", value1->valuestring); } cJSON* value2 = cJSON_GetObjectItemCaseSensitive(root, "key2"); if (cJSON_IsNumber(value2)) { printf("Value of key2: %d\n", value2->valueint); } cJSON* value3 = cJSON_GetObjectItemCaseSensitive(root, "key3"); if (cJSON_IsArray(value3)) { int array_size = cJSON_GetArraySize(value3); printf("Size of key3 array: %d\n", array_size); for (int i = 0; i < array_size; i++) { cJSON* item = cJSON_GetArrayItem(value3, i); if (cJSON_IsNumber(item)) { printf("Element %d: %d\n", i, item->valueint); } } } cJSON_Delete(root); return 0; } ``` 在上面的示例中,我们首先定义了一个JSON字符串 `json_data`,其中包含了多个对。然后使用 `cJSON_Parse` 函数将其解析为一个 `cJSON` 对象 `root`。接下来,我们使用 `cJSON_GetObjectItemCaseSensitive` 函数来获取特定,并使用相应的类型检查函数来验证的类型。根据的类型,我们可以使用不同的函数来获取,并将其打印出来。 请注意,在使用此示例代码之前,您需要先下载并编译CJSON库,并将其头文件和库文件正确地包含到您的项目中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值