【Linux C编程】第二十章XML-JSON之示例
【摘要】 示例代码
(3)示例
生成下面的JSON
car.json
1 {
2 "奔驰": {
3 "factory": "一汽大众",
4 "last": 31,
5 "price": 83,
6 "sell": 49,
7 "sum": 80,
8 "other": [124, "hello, world", false]
9 }
10 }
create_car.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "cJSON.h"
4
5 int main(int argc, const char* argv[])
6 {
7 // 创建json对象
8 cJSON* obj = cJSON_CreateObject();
9
10 // 创建子对象 - 品牌
11 cJSON* brand = cJSON_CreateObject();
12 // 添加键值对
13 cJSON_AddItemToObject(brand, "factory", cJSON_CreateString("一汽大众"));
14 cJSON_AddItemToObject(brand, "last", cJSON_CreateNumber(31));
15 cJSON_AddItemToObject(brand, "price", cJSON_CreateNumber(83));
16 cJSON_AddItemToObject(brand, "sell", cJSON_CreateNumber(49));
17 cJSON_AddItemToObject(brand, "sum", cJSON_CreateNumber(80));
18
19 // 创建json数组
20 cJSON* array = cJSON_CreateArray();
21 cJSON_AddItemToArray(array, cJSON_CreateNumber(124));
22 cJSON_AddItemToArray(array, cJSON_CreateString("hello, world"));
23 cJSON_AddItemToArray(array, cJSON_CreateBool(0));
24 cJSON_AddItemToObject(brand, "other", array);
25
26 cJSON_AddItemToObject(obj, "奔驰", brand);
27
28 // 格式化json对象
29 char* text = cJSON_Print(obj);
30 FILE* fp = fopen("car.json", "w");
31 fwrite(text, 1, strlen(text), fp);
32 fclose(fp);
33
34 return 0;
35 }
源码:
cJSON.c
cJSON.h
parsejson.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "cJSON.h"
4
5 int main(int argc, const char* argv[])
6 {
7 if(argc < 2)
8 {
9 printf("./a.out jsonfile\n");
10 return 0;
11 }
12
13 // 加载json文件
14 FILE* fp = fopen(argv[1], "r");
15 char buf[1024] = {0};
16 fread(buf, 1, sizeof(buf), fp);
17 cJSON* root = cJSON_Parse(buf);
18
19 cJSON* subobj = cJSON_GetObjectItem(root, "奔驰");
20 // 判断对象是否存在
21 if( subobj )
22 {
23 // 获取子对象
24 cJSON* factory = cJSON_GetObjectItem(subobj, "factory");
25 cJSON* last = cJSON_GetObjectItem(subobj, "last");
26 cJSON* price = cJSON_GetObjectItem(subobj, "price");
27 cJSON* sell = cJSON_GetObjectItem(subobj, "sell");
28 cJSON* sum = cJSON_GetObjectItem(subobj, "sum");
29 cJSON* other = cJSON_GetObjectItem(subobj, "other");
30
31 // 打印value值
32 printf("奔驰:\n");
33 printf(" factory: %s\n", cJSON_Print(factory));
34 printf(" last: %s\n", cJSON_Print(last));
35 printf(" price: %s\n", cJSON_Print(price));
36 printf(" sell: %s\n", cJSON_Print(sell));
37 printf(" sum: %s\n", cJSON_Print(sum));
38
39 // 打印数组内容
40 printf(" other:\n");
41 if(other->type == cJSON_Array)
42 {
43 for(int i=0; i<cJSON_GetArraySize(other); ++i)
44 {
45 cJSON* node = cJSON_GetArrayItem(other, i);
46 // 判断数据类型
47 if(node->type == cJSON_String)
48 {
49 printf(" %s \n", node->valuestring);
50 }
51 if(node->type == cJSON_Number)
52 {
53 printf(" %d\n", node->valueint);
54 }
55 if(node->type == cJSON_True)
56 {
57 printf(" %d\n", node->valueint);
58 }
59 if(node->type == cJSON_False)
60 {
61 printf(" %d\n", node->valueint);
62 }
63 }
64 }
65 }
66
67 cJSON_Delete(root);
68 fclose(fp);
69
70
71 return 0;
72 }
注意:上面的cJSON.c和cJSON.h是 cJSON-master.zip包里面的。
cJSON结构体:
#define cJSON_Invalid (0)
#define cJSON_False (1 << 0)
#define cJSON_True (1 << 1)
#define cJSON_NULL (1 << 2)
#define cJSON_Number (1 << 3)
#define cJSON_String (1 << 4)
#define cJSON_Array (1 << 5)
#define cJSON_Object (1 << 6)
#define cJSON_Raw (1 << 7)
/* The cJSON structure: */
typedef struct cJSON
{
struct cJSON *next;
struct cJSON *prev;
struct cJSON *child;
int type;
char *valuestring;
int valueint;
double valuedouble;
char *string;
} cJSON;
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)