夜深了 人也静了 来写一篇博文吧
此次博主介绍 cjson
这款类库的使用,博主用的是 vs2022
下载 cjson
https://github.com/DaveGamble/cJSON
直接复制 cJSON.h
cJSON.c
文件到项目中
中文乱码问题
首先将文件保存成 UTF8 带签名 65001
也就是 UTF8-BOM
其次使用 Unicode字符集
并且添加 /utf8
编译选项
对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| //设置控制台编码 #include <Windows.h> 65001 = utf-8 SetConsoleOutputCP(65001); cJSON* obj = cJSON_CreateObject();
cJSON* nameVal = cJSON_CreateString("张三是haha3596.."); cJSON* ageVal = cJSON_CreateNumber(135);
cJSON_AddItemToObject(obj, "name", nameVal); cJSON_AddItemToObject(obj, "age", ageVal);
char* rendered = cJSON_Print(obj); printf("\n %s \n", rendered);
free(rendered); cJSON_Delete(obj);
|
数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| SetConsoleOutputCP(65001);
cJSON* arr = cJSON_CreateArray();
cJSON* arrItem = cJSON_CreateObject(); cJSON* name = cJSON_CreateString("张三"); cJSON* age = cJSON_CreateNumber(50); cJSON_AddItemToObject(arrItem, "name", name); cJSON_AddItemToObject(arrItem, "age", age); cJSON_AddItemToArray(arr, arrItem);
cJSON* arrItem2 = cJSON_CreateObject(); cJSON* name2 = cJSON_CreateString("李四"); cJSON* age2 = cJSON_CreateNumber(80); cJSON_AddItemToObject(arrItem2, "name", name2); cJSON_AddItemToObject(arrItem2, "age", age2); cJSON_AddItemToArray(arr, arrItem2);
char* rendered = cJSON_Print(arr); printf("\n %s \n", rendered);
free(rendered); cJSON_Delete(arr);
|
对象嵌套对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| SetConsoleOutputCP(65001); cJSON* obj = cJSON_CreateObject(); cJSON* nameVal = cJSON_CreateString("马鞍斯"); cJSON* ageVal = cJSON_CreateNumber(10); cJSON_AddItemToObject(obj, "name", nameVal); cJSON_AddItemToObject(obj, "age", ageVal);
cJSON* car = cJSON_CreateObject(); cJSON* colorVal = cJSON_CreateString("黑色"); cJSON* modelVal = cJSON_CreateString("一汽大众"); cJSON_AddItemToObject(car, "color", colorVal); cJSON_AddItemToObject(car, "model", modelVal);
cJSON_AddItemToObject(obj, "car", car);
char* rendered = cJSON_Print(obj); printf("\n %s \n", rendered);
free(rendered); cJSON_Delete(obj);
|
对象嵌套数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| SetConsoleOutputCP(65001);
cJSON* obj = cJSON_CreateObject(); cJSON* nameVal = cJSON_CreateString("马鞍斯"); cJSON* ageVal = cJSON_CreateNumber(10); cJSON_AddItemToObject(obj, "name", nameVal); cJSON_AddItemToObject(obj, "age", ageVal);
cJSON* arr = cJSON_CreateArray(); cJSON* car = cJSON_CreateObject(); cJSON* colorVal = cJSON_CreateString("黑色"); cJSON* modelVal = cJSON_CreateString("一汽大众"); cJSON_AddItemToObject(car, "color", colorVal); cJSON_AddItemToObject(car, "model", modelVal); cJSON_AddItemToObject(arr, "car", car);
cJSON* car2 = cJSON_CreateObject(); cJSON* colorVal2 = cJSON_CreateString("白色"); cJSON* modelVal2 = cJSON_CreateString("上海大众"); cJSON_AddItemToObject(car2, "color", colorVal2); cJSON_AddItemToObject(car2, "model", modelVal2); cJSON_AddItemToObject(arr, "car", car2);
cJSON_AddItemToObject(obj, "cars", arr);
char* rendered = cJSON_Print(obj); printf("\n %s \n", rendered);
free(rendered); cJSON_Delete(obj);
|
解析json字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| { "name":"张柳", "age":50, "address":"浙江苏州", "cars":[ { "color":"黑色", "brand":"上海大众" }, { "color":"白色", "brand":"特拉斯" } ] }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| SetConsoleOutputCP(65001);
char * jsonStr = "{\"name\":\"张柳\",\"age\":50,\"address\":\"浙江苏州\",\"cars\":[{\"color\":\"黑色\",\"brand\":\"上海大众\"},{\"color\":\"白色\",\"brand\":\"特拉斯\"}]}"; cJSON* root = cJSON_Parse(jsonStr);
cJSON* name = cJSON_GetObjectItem(root, "name"); cJSON* age = cJSON_GetObjectItem(root, "age"); printf("name: %s\n",name->valuestring); printf("age: %d\n", age->valueint);
//遍历json数组 cJSON* cars = cJSON_GetObjectItem(root,"cars");
for (int i = 0; i < cJSON_GetArraySize(cars); i++) { cJSON* item = cJSON_GetArrayItem(cars, i); cJSON* color = cJSON_GetObjectItem(item, "color"); cJSON* brand = cJSON_GetObjectItem(item, "brand"); printf("color=%s,brand=%s\n", color->valuestring, brand->valuestring); }
cJSON_Delete(root);
|