cjson库(cjson库函数)

cjson库(cjson库函数)1 include stdio h 2 include cJSON h 3 4 int main 5 FILE fp NULL 6 cJSON json 7 char out 8 char line 1024 0 9 10 if NULL fp fopen https www cnblogs com piaoyang p test ply r 11 while stdio h



 1 #include <stdio.h>
 2 #include "cJSON.h"
 3 
 4 int main() {
 5     FILE            *fp = NULL;
 6     cJSON           *json;
 7     char            *out;
 8     char            line[1024] = {0};
 9 
10     if(NULL != (fp = fopen("https://www.cnblogs.com/piaoyang/p/test.ply", "r"))) {
11         while (NULL != fgets(line, sizeof(line), fp)) {
12             json=cJSON_Parse(line); //获取整个大的句柄
13             out=cJSON_Print(json);  //这个是可以输出的。为获取的整个json的值
14             cJSON *arrayItem = cJSON_GetObjectItem(json,"syslog_db"); //获取这个对象成员
15             cJSON *object = cJSON_GetArrayItem(arrayItem,0);   //因为这个对象是个数组获取,且只有一个元素所以写下标为0获取
16             
17         /*下面就是可以重复使用cJSON_GetObjectItem来获取每个成员的值了*/
18             cJSON *item = cJSON_GetObjectItem(object,"db_user");  //
19             printf("db_user:%s
",item->valuestring);
20 
21             item = cJSON_GetObjectItem(object,"db_password");
22             printf("db_password:%s
",item->valuestring);
23 
24             item = cJSON_GetObjectItem(object,"db_type");
25             printf("db_type:%s
",item->valuestring);
26 
27             item = cJSON_GetObjectItem(object,"db_ip");
28             printf("db_ip:%s
",item->valuestring);
29 
30             item = cJSON_GetObjectItem(object,"db_port");
31             printf("db_port:%s
",item->valuestring);
32 
33             item = cJSON_GetObjectItem(object,"db_name");
34             printf("db_name:%s
",item->valuestring);
35 
36             item = cJSON_GetObjectItem(object,"sql");
37             printf("db_sql:%s
",item->valuestring);
38         
39 
40        /*这里这个是直接可以获取值的*/
41             arrayItem = cJSON_GetObjectItem(json,"syslog_enable");
42             printf("%s
",arrayItem->valuestring);
43         }
44     }
45         cJSON_Delete(json);
46     close(fp);
47 }    
编程小号
上一篇 2026-02-23 18:01
下一篇 2026-02-23 18:27

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ri-ji/47640.html