一、cJSON简介
JSON是一种文本数据交换格式,独立与语言存在。
基本格式为key:value形式,还有数组。
例如:
{
"action":"set",
"mac":["00:17:62:51:29:21"],
"password":"admin",
"dataList":
[{
"url_path":"/",
"method":"POST",
"Template":"register",
"data":
{
"action":"mode_set",
"mode":"1",
"username":"prouser",
"password":"admin"
}
}]
}
cJSON是一个c语言编写的JSON解释器,仅有一个和文件。
附链接:cJSON
二、cJSON用法
1.构造/解析cJSON
示例:
{
"action":"mode_set",
"mode":1
}
构造数据:
cJSON *JsData = cJSON_CreateObject();
cJSON_AddStringToObject(JsData, "action", "mode_set");
cJSON_AddNumberToObject(JsData, "mode", 1);
解析数据:
char c1Data[100] =
"
{
\"action\":\"cloud_set\",
\"mode\":1
}
"
cJSON *JsData = NULL;
cJSON *pValue = NULL;
JsData = cJSON_Parse(c1Data);
pValue = cJSON_GetObjectItem(JsData, "action");
printf("action : %s \r\n", pValue->valuestring);
pValue = cJSON_GetObjectItem(JsData, "mode");
printf("mode : %d \r\n", pValue->valuestring);
扩展:
组数据时,还有以下api
cJSON_AddBoolToObject(object, key, value);
cJSON_AddFalseToObject(object, key);
cJSON_AddTrueToObject(object, key);
cJSON_AddNullToObject(object, null);
2.嵌套JSON
{
"action":"mode_set",
"mode":1
"datalist":
{
"username":"admin",
"password":"admin"
}
}
组数据:
cJSON *JsDataList = cJSON_CreateObject();
cJSON_AddItemToObject(JsData, JsDataList);
cJSON_AddStringToObject(JsDataList, "username", "admin");
cJSON_AddStringToObject(JsDataList, "password", "admin");
解析数据:
cJSON *JsDataList = NULL;
cJSON *pValue = NULL;
JsDataList = cJSON_GetObjectItem(JsData, "datalist");
pValue = cJSON_GetObjectItem(datalist, "username");
printf("username : %s \r\n", pValue->valuestring);
pValue = cJSON_GetObjectItem(datalist, "password");
printf("password : %s \r\n", pValue->valuestring);
3.数组
示例:
{
"action":"mode_set",
"mode":1,
"datalist":
{
"username":"admin",
"password":"admin"
}
"register":
["{
"status":"0",
"msg":"Success."
}"]
}
组数据:
cJSON *JsRegisterArr = cJSON_CreateArray();
cJSON *JsRegisterObj = cJSON_CreateObject();
cJSON_AddItemToObject(JsData, "register", JsRegisterArr);
cJSON_AddItemToArray(JsRegisterArr, JsRegisterObj);
cJSON_AddStringToObject(JsRegisterObj, "status", "0");
cJSON_AddStringToObject(JsRegisterObj, "msg", "Success");
解析数据:
cJSON *JsRegisterArr = NULL;
cJSON *JsRegisterObj = NULL;
cJSON *pValue = NULL;
UINT4 u4DataSize = 0;
JsRegisterArr = cJSON_GetObjectItem(JsData, "register");
u4DataSize = cJSON_GetArraySize(JsRegisterArr);
for (UINT4 u4Index = 0; u4Index < u4DataSize; u4Index ++)
{
JsRegisterObj = cJSON_GetArrayItem(JsRegisterArr, u4Index)
pValue = cJSON_GetObjectItem(JsRegisterObj, "status");
printf("status : %s \r\n", pValue->valuestring);
pValue = cJSON_GetObjectItem(JsRegisterObj, "msg");
printf("msg : %s \r\n", pValue->valuestring);
}
4.替换和删除cJSON
示例:
{
"action":"mode_set",
"mode":1
"datalist":
{
"username":"admin",
"password":"admin"
}
}
替换password:
cJSON *JsDataList = NULL;
cJSON_ReplaceItemInObject(JsDataList, "password", "admin123");
删除password:
cJSON_DeleteItemFromObject(JsDataList, "password");
5.格式化输出cJSON数据
UINT1 *pu1Data = NULL;
UINT1 au1Data[1024] = {0};
pu1Data = cJSON_PrintUnformatted(JsData);
SPRINTF(au1Data, pu1Data);
6.内存申请与释放
以下函数调用时都会申请内存,如果不释放内存会造成内存泄露:
cJSON_Parse();
cJSON_CreateObject();
cJSON_CreateArray();
cJSON_Print();
cJSON_PrintUnformatted();
释放内存:
cJSON_Delete();
cJSON_free();
用法解释:
1.调用cJSON_Parse,cJSON_CreateObject,cJSON_CreateArray时,使用cJSON_Delete释放内存,
比如JsData = cJSON_CreateObject(), DataListArr = cJSON_CreateArray();cJSON_AddItemToArray(DataListArr, JsData);
这种情况只要cJSON_Delete(JsData), 这样也会同时释放DataListArr的内存。
2.如果是调用cJSON_Print,cJSON_PrintUnformatted时,则使用cJSON_free();
建议:
因为涉及申请内存及释放内存,所以在每次creat一个cJSON后,使用前都要判空。
cJSON *JsData = NULL;
JsData = cJSON_CreateObject();
if (JsData == NULL)
return FAILURE;