這篇文章給大家分享的是有關C/C++中CJSON怎么用的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
cJSON 是一個超輕巧,攜帶方便,單文件,可以作為 ANSI-C 標準的 JSON 解析器,是一個用C語言編寫的簡單好用的JSON解析器;它只包含一個C文件和一個頭文件,可以非常容易集成到自己工程項目中。
并且cJSON是用ANSI C(C89)編寫的,可以兼容所有支持C語言的平臺和編譯器。
cJSON下載地址: https://sourceforge.net/projects/cjson/
cJSON的GitHub倉庫地址:https://github.com/DaveGamble/cJSON

JSON是JavaScript Object Notation(JavaScript對象表示法),是一種輕量級的數據交換格式。
JSON主要是用來存儲和交換文本信息,類似XML格式;但是JSON比XML更小、更快,更易解析。
JSON是基于ECMAScript (歐洲計算機協會制定的js規范)的一個子集,采用完全獨立于編程語言的文本格式來存儲和表示數據。
簡潔和清晰的層次結構使得 JSON 成為理想的數據交換語言。 易于人閱讀和編寫,同時也易于機器解析和生成,并有效地提升網絡傳輸效率。 比如: Web服務器接口基本都是采用JSON反饋數據,采用JSON格式字符串來描述符信息。 JSON文件的后綴一般是.json,這個只是為了方便辨識。
簡單的說,JSON就是按照指定格式序列化的字符串,就算不使用任何現成的解析庫,自己也可以按照正常解析字符串的思路去解析;有現成的標準JSON解析庫,那就大大減輕了我們的工作量。
JSON格式的數據示例: 這是表示當前時間的JSON字符串
{
"success": "1",
"result": {
"timestamp": "1631849514",
"datetime_1": "2021-09-17 20:31:54",
"datetime_2": "2021年09月17日 20時31分54秒",
"week_1": "5",
"week_2": "星期五",
"week_3": "周五",
"week_4": "Friday"
}
}JSON格式的數據示例: 這是表示未來幾天天氣預報的json字符串
{
"success": "1",
"result": [
{
"weaid": "1",
"days": "2021-09-17",
"week": "星期五",
"cityno": "beijing",
"citynm": "北京",
"cityid": "101010100",
"temperature": "26℃/17℃",
"humidity": "0%/0%",
"weather": "晴",
"weather_icon": "http://api.k780.com/upload/weather/d/0.gif",
"weather_icon1": "http://api.k780.com/upload/weather/n/0.gif",
"wind": "北風轉西南風",
"winp": "小于3級",
"temp_high": "26",
"temp_low": "17",
"humi_high": "0",
"humi_low": "0",
"weatid": "1",
"weatid1": "1",
"windid": "8",
"winpid": "0",
"weather_iconid": "0",
"weather_iconid1": "0"
},
{
"weaid": "1",
"days": "2021-09-18",
"week": "星期六",
"cityno": "beijing",
"citynm": "北京",
"cityid": "101010100",
"temperature": "25℃/17℃",
"humidity": "0%/0%",
"weather": "多云",
"weather_icon": "http://api.k780.com/upload/weather/d/1.gif",
"weather_icon1": "http://api.k780.com/upload/weather/n/1.gif",
"wind": "西南風",
"winp": "小于3級",
"temp_high": "25",
"temp_low": "17",
"humi_high": "0",
"humi_low": "0",
"weatid": "2",
"weatid1": "2",
"windid": "5",
"winpid": "0",
"weather_iconid": "1",
"weather_iconid1": "1"
},
{
"weaid": "1",
"days": "2021-09-19",
"week": "星期日",
"cityno": "beijing",
"citynm": "北京",
"cityid": "101010100",
"temperature": "19℃/15℃",
"humidity": "0%/0%",
"weather": "小雨轉中雨",
"weather_icon": "http://api.k780.com/upload/weather/d/7.gif",
"weather_icon1": "http://api.k780.com/upload/weather/n/8.gif",
"wind": "西南風轉北風",
"winp": "小于3級轉小于3級",
"temp_high": "19",
"temp_low": "15",
"humi_high": "0",
"humi_low": "0",
"weatid": "8",
"weatid1": "9",
"windid": "5",
"winpid": "0",
"weather_iconid": "7",
"weather_iconid1": "8"
},
{
"weaid": "1",
"days": "2021-09-20",
"week": "星期一",
"cityno": "beijing",
"citynm": "北京",
"cityid": "101010100",
"temperature": "26℃/16℃",
"humidity": "0%/0%",
"weather": "多云轉晴",
"weather_icon": "http://api.k780.com/upload/weather/d/1.gif",
"weather_icon1": "http://api.k780.com/upload/weather/n/0.gif",
"wind": "北風",
"winp": "3-4級轉3-4級",
"temp_high": "26",
"temp_low": "16",
"humi_high": "0",
"humi_low": "0",
"weatid": "2",
"weatid1": "1",
"windid": "8",
"winpid": "1",
"weather_iconid": "1",
"weather_iconid1": "0"
},
{
"weaid": "1",
"days": "2021-09-21",
"week": "星期二",
"cityno": "beijing",
"citynm": "北京",
"cityid": "101010100",
"temperature": "27℃/16℃",
"humidity": "0%/0%",
"weather": "晴",
"weather_icon": "http://api.k780.com/upload/weather/d/0.gif",
"weather_icon1": "http://api.k780.com/upload/weather/n/0.gif",
"wind": "西北風轉北風",
"winp": "小于3級",
"temp_high": "27",
"temp_low": "16",
"humi_high": "0",
"humi_low": "0",
"weatid": "1",
"weatid1": "1",
"windid": "7",
"winpid": "0",
"weather_iconid": "0",
"weather_iconid1": "0"
},
{
"weaid": "1",
"days": "2021-09-22",
"week": "星期三",
"cityno": "beijing",
"citynm": "北京",
"cityid": "101010100",
"temperature": "26℃/18℃",
"humidity": "0%/0%",
"weather": "多云",
"weather_icon": "http://api.k780.com/upload/weather/d/1.gif",
"weather_icon1": "http://api.k780.com/upload/weather/n/1.gif",
"wind": "北風轉東北風",
"winp": "小于3級",
"temp_high": "26",
"temp_low": "18",
"humi_high": "0",
"humi_low": "0",
"weatid": "2",
"weatid1": "2",
"windid": "8",
"winpid": "0",
"weather_iconid": "1",
"weather_iconid1": "1"
},
{
"weaid": "1",
"days": "2021-09-23",
"week": "星期四",
"cityno": "beijing",
"citynm": "北京",
"cityid": "101010100",
"temperature": "24℃/16℃",
"humidity": "0%/0%",
"weather": "多云",
"weather_icon": "http://api.k780.com/upload/weather/d/1.gif",
"weather_icon1": "http://api.k780.com/upload/weather/n/1.gif",
"wind": "東北風",
"winp": "小于3級",
"temp_high": "24",
"temp_low": "16",
"humi_high": "0",
"humi_low": "0",
"weatid": "2",
"weatid1": "2",
"windid": "1",
"winpid": "0",
"weather_iconid": "1",
"weather_iconid1": "1"
}
]
}JSON里就分為兩種結構: 對象和數組,通過這兩種結構可以表示各種復雜的結構。
JSON語法規則
1. 大括號 { } 用來保存對象
2. 中括號 [ ] 用來保存數組,數組里也可以包含多個對象,對象里又可以包含數組,可以嵌套
3. JSON的值表示語法: key : value --> "width": 1280
4. 多個數據由逗號分隔: {"width": 1920,"height": 1080}
JSON值可以是以下幾種類型:
1. 數字(整數或浮點數)
2. 字符串(在雙引號中)
3. 邏輯值(true 或 false)
4. 數組(在中括號中)
5. 對象(在大括號中)
6. null (空值)
這是下載下來的cJSON源文件,將它加到自己工程中即可。

我這里使用VS2017建立工程,演示實例。

建好工程之后,將文件添加到工程里:

在VS2017里使用C語言的字符串處理函數會報錯,提示不安全;
1>d:\linux-share-dir\vs2017\console_cjsontest\console_cjsontest\cjson.c(155): error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
解決辦法是:找到【項目屬性】,點擊【C++】里的【預處理器】,對【預處理器】進行編輯,在里面加入一段代碼:_CRT_SECURE_NO_WARNINGS。

接下來目標是使用cJSON創建出下面這樣一個JSON格式數據:
{
"text": "我是一個字符串數據",
"number": 666,
"state1": false,
"state2": true,
"state3": null
}示例代碼如下:

#include <iostream>
//因為當前工程使用的是cpp后綴文件,引用C語言的文件需要使用下面的這種方式
extern "C" {
#include <string.h>
#include <stdio.h>
#include "cJSON.h"
}
int main()
{
//1. 創建cJSON對象
cJSON* root = cJSON_CreateObject();
//2. 創建數據
cJSON_AddStringToObject(root, "text","我是一個字符串數據");
cJSON_AddNumberToObject(root,"number",666);
cJSON_AddBoolToObject(root, "state1", cJSON_False);
cJSON_AddBoolToObject(root, "state2", cJSON_True);
cJSON_AddNullToObject(root, "state3");
//3. 打印生成的結果
char *json_data=cJSON_PrintUnformatted(root);
printf("%s\n",json_data);
//4. 釋放空間
cJSON_Delete(root);
return 0;
}
#include <iostream>
//因為當前工程使用的是cpp后綴文件,引用C語言的文件需要使用下面的這種方式
extern "C" {
#include <string.h>
#include <stdio.h>
#include "cJSON.h"
}
//將要解析的JSON數據.
char data[] =
"{"
"\"text\": \"我是一個字符串數據\","
"\"number\" : 666,"
"\"state1\" : false,"
"\"state2\" : true,"
"\"state3\" : null"
"}";
int main()
{
//1. 載入JSON數據
cJSON* root = cJSON_Parse(data);
if (root == NULL)return 0;
//2. 解析字段
cJSON* item;
item=cJSON_GetObjectItem(root,"text");
if (item)
{
printf("text=%s\n",item->valuestring);
}
item = cJSON_GetObjectItem(root, "number");
if (item)
{
printf("text=%d\n", item->valueint);
}
item = cJSON_GetObjectItem(root, "state1");
if (item)
{
printf("state1=%d\n", item->valueint);
}
item = cJSON_GetObjectItem(root, "state2");
if (item)
{
printf("state2=%d\n", item->valueint);
}
item = cJSON_GetObjectItem(root, "state3");
if (item)
{
printf("state3=%d\n", item->valueint);
}
//3. 釋放空間
cJSON_Delete(root);
return 0;
}
目標: 使用cJSON創建出下面這樣一個JSON格式數據
{
"data1": {
"text": "我是一個字符串數據1",
"number": 666,
"state1": false,
"state2": true,
"state3": null
},
"data2": {
"text": "我是一個字符串數據2",
"number": 666,
"state1": false,
"state2": true,
"state3": null
}
}#include <iostream>
//因為當前工程使用的是cpp后綴文件,引用C語言的文件需要使用下面的這種方式
extern "C" {
#include <string.h>
#include <stdio.h>
#include "cJSON.h"
}
int main()
{
//1. 創建cJSON對象
cJSON* root = cJSON_CreateObject();
//2. 創建對象數據1
cJSON* item1 = cJSON_CreateObject();
cJSON_AddStringToObject(item1, "text","我是一個字符串數據1");
cJSON_AddNumberToObject(item1,"number",666);
cJSON_AddBoolToObject(item1, "state1", cJSON_False);
cJSON_AddBoolToObject(item1, "state2", cJSON_True);
cJSON_AddNullToObject(item1, "state3");
cJSON_AddItemToObject(root, "data1", item1);
//3. 創建對象數據2
cJSON* item2 = cJSON_CreateObject();
cJSON_AddStringToObject(item2, "text", "我是一個字符串數據2");
cJSON_AddNumberToObject(item2, "number", 666);
cJSON_AddBoolToObject(item2, "state1", cJSON_False);
cJSON_AddBoolToObject(item2, "state2", cJSON_True);
cJSON_AddNullToObject(item2, "state3");
cJSON_AddItemToObject(root, "data2", item2);
//3. 打印生成的結果
char *json_data=cJSON_PrintUnformatted(root);
printf("%s\n",json_data);
//4. 釋放空間
cJSON_Delete(root);
return 0;
}#include <iostream>
//因為當前工程使用的是cpp后綴文件,引用C語言的文件需要使用下面的這種方式
extern "C" {
#include <string.h>
#include <stdio.h>
#include "cJSON.h"
}
//將要解析的JSON數據.
char data[] =
"{"
"\"data1\": {"
"\"text\": \"我是一個字符串數據1\","
"\"number\" : 666,"
"\"state1\" : false,"
"\"state2\" : true,"
"\"state3\" : null"
"},"
"\"data2\": {"
"\"text\":\"我是一個字符串數據2\","
"\"number\" : 666,"
"\"state1\" : false,"
"\"state2\" : true,"
"\"state3\" : null"
"}"
"}";
int main()
{
//1. 載入JSON數據
cJSON* root = cJSON_Parse(data);
if (root == NULL)return 0;
//2. 解析字段
cJSON* item;
item=cJSON_GetObjectItem(root,"data1");
if (item)
{
cJSON *obj;
obj=cJSON_GetObjectItem(item, "text");
if (obj)
{
printf("text=%s\n", obj->valuestring);
}
obj=cJSON_GetObjectItem(item, "number");
if (obj)
{
printf("number=%d\n", obj->valueint);
}
obj = cJSON_GetObjectItem(item, "state1");
if (obj)
{
printf("state1=%d\n", obj->valueint);
}
obj = cJSON_GetObjectItem(item, "state2");
if (obj)
{
printf("state2=%d\n", obj->valueint);
}
obj = cJSON_GetObjectItem(item, "state3");
if (obj)
{
printf("state3=%d\n", obj->valueint);
}
}
item = cJSON_GetObjectItem(root, "data2");
if (item)
{
cJSON *obj;
obj = cJSON_GetObjectItem(item, "text");
if (obj)
{
printf("text=%s\n", obj->valuestring);
}
obj = cJSON_GetObjectItem(item, "number");
if (obj)
{
printf("number=%d\n", obj->valueint);
}
obj = cJSON_GetObjectItem(item, "state1");
if (obj)
{
printf("state1=%d\n", obj->valueint);
}
obj = cJSON_GetObjectItem(item, "state2");
if (obj)
{
printf("state2=%d\n", obj->valueint);
}
obj = cJSON_GetObjectItem(item, "state3");
if (obj)
{
printf("state3=%d\n", obj->valueint);
}
}
//3. 釋放空間
cJSON_Delete(root);
return 0;
}
目標: 使用cJSON創建出下面這樣一個JSON格式數據
{
"text": [
{
"width": 1280,
"height": 720
},
{
"width": 1920,
"height": 1080
},
{
"width": 3840,
"height": 2160
}
]
}#include <iostream>
//因為當前工程使用的是cpp后綴文件,引用C語言的文件需要使用下面的這種方式
extern "C" {
#include <string.h>
#include <stdio.h>
#include "cJSON.h"
}
int main()
{
cJSON *width = NULL;
cJSON *height = NULL;
int i;
const unsigned int resolution_numbers[3][2] = {
{1280, 720},
{1920, 1080},
{3840, 2160}
};
//1. 創建cJSON對象
cJSON* root = cJSON_CreateObject();
//2. 創建數組對象
cJSON *array = cJSON_CreateArray();
cJSON_AddItemToObject(root, "text", array);
for (i = 0; i < (sizeof(resolution_numbers) / (2 * sizeof(int))); ++i)
{
cJSON *obj = cJSON_CreateObject();
cJSON_AddItemToArray(array, obj);
width = cJSON_CreateNumber(resolution_numbers[i][0]);
cJSON_AddItemToObject(obj, "width", width);
height = cJSON_CreateNumber(resolution_numbers[i][1]);
cJSON_AddItemToObject(obj, "height", height);
}
//3. 打印生成的結果
char *json_data=cJSON_PrintUnformatted(root);
printf("%s\n",json_data);
//4. 釋放空間
cJSON_Delete(root);
return 0;
}#include <iostream>
//因為當前工程使用的是cpp后綴文件,引用C語言的文件需要使用下面的這種方式
extern "C" {
#include <string.h>
#include <stdio.h>
#include "cJSON.h"
}
//將要解析的JSON數據.
char data[] =
"{"
"\"text\": ["
"{"
"\"width\": 1280,"
"\"height\" : 720"
"},"
"{"
"\"width\": 1920,"
"\"height\" : 1080"
"},"
"{"
"\"width\": 3840,"
"\"height\" : 2160"
"}"
"]"
"}";
int main()
{
//1. 載入JSON數據
cJSON* root = cJSON_Parse(data);
if (root == NULL)return 0;
//2. 解析字段
cJSON* item;
int i;
item = cJSON_GetObjectItem(root, "text");
if (item)
{
//獲取數組的大小
int ArraySize = cJSON_GetArraySize(item);
//解析數組的里的每個成員
for (i = 0; i < ArraySize; i++)
{
//取出數組下標對象
cJSON *array_item = cJSON_GetArrayItem(item, i);
if (array_item == NULL)continue;
//解析數據
cJSON *obj = cJSON_GetObjectItem(array_item, "width");
if (obj)
{
printf("width=%d\n",obj->valueint);
}
obj = cJSON_GetObjectItem(array_item, "height");
if (obj)
{
printf("height=%d\n", obj->valueint);
}
}
}
//3. 釋放空間
cJSON_Delete(root);
return 0;
}
感謝各位的閱讀!關于“C/C++中CJSON怎么用”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。