JSON介绍、cJSON的使⽤(解析和构造)
1 JSON介绍
JSON(JavaScript Object Notation, JS ) 是⼀种轻量级的数据交换格式。
1.1 采⽤完全独⽴于编程语⾔的⽂本格式来存储和表⽰数据;
1.2 简洁和清晰的层次结构使得 JSON 成为理想的数据交换语⾔;
1.3 易于⼈阅读和编写,同时也易于机器解析和⽣成,并有效地提升⽹络传输效率。
2 JSON 语法
2.1 语法规则
1. 数据在 “名称/值 对” 中
2. 数据由 逗号 , 分隔
3. ⼤括号 {} 保存 对象
4. 中括号 [] 保存数组,数组可以包含多个对象
c++string类型2.2 对象 object
对象(object):⼀个对象以 ”{“ 开始,并以 ”}” 结束。⼀个对象包含⼀系列 ⾮排序的名称/值对,每个名称/值对之间使⽤”,”分隔。
{"Mode":"hello"}
2.3 名称/值 item
名称/值(item):名称和值之间使⽤”:”隔开。名称是⼀个字符串;值 (value)可以是双引号括起来的字符串(string)、数值(number)、true、false、null、对象(object)或者数组(array)
"name": value
2.4 JSON 值 value
JSON 值可以是:
1、数字(整数int 或 浮点数 double)
{"opt":1234}/* 值是 1234 */
2、字符串(在双引号中)
{"Mode":"hello"}/* 值是字符串 "hello" */
3、逻辑值(true 或 false)
{"status":true}
4、数组(在中括号中)
{""Mode":[1,1]}
5、对象(在⼤括号中)
{"opt":{"status":true}}
6、null
{"opt":null}
3 cJSON
cJSON,⽬前来说,就只有两个⽂件,⼀个cJSON.c ⼀个cJSON.h⽂件。
3.1 cJSON结构体
/* The cJSON structure: */
typedef struct cJSON
{
/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
struct cJSON *next;
struct cJSON *prev;
/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
struct cJSON *child;
/* The type of the item, as above. */
int type;
/* The item's string, if type==cJSON_String  and type == cJSON_Raw */
char*valuestring;/*** 存放字符串类型的值 ***/
/* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
int valueint;/*** 存放 int类型的值 ***/
/* The item's number, if type==cJSON_Number */
double valuedouble;/*** 存放 double类型的值 ***/
/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
char*string;/*** 名称字符串 ***/
} cJSON;
3.2 cJSON.h 中的API
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */
extern cJSON *cJSON_Parse(const char*value);//从给定的json字符串中得到cjson对象
/* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */
extern char*cJSON_Print(cJSON *item);//从cjson对象中获取有格式的json对象
/* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */
extern char*cJSON_PrintUnformatted(cJSON *item);//从cjson对象中获取⽆格式的json对象
/* Delete a cJSON entity and all subentities. */
extern void cJSON_Delete(cJSON *c);//删除cjson对象,释放链表占⽤的内存空间
CJSON_PUBLIC(cJSON *)cJSON_CreateArray(void);
CJSON_PUBLIC(cJSON *)cJSON_CreateObject(void);
/* Append item to the specified array/object. */
CJSON_PUBLIC(void)cJSON_AddItemToArray(cJSON *array, cJSON *item);
CJSON_PUBLIC(void)cJSON_AddItemToObject(cJSON *object,const char*string, cJSON *item);
/* Helper functions for creating and adding items to an object at the same time.
* They return the added item or NULL on failure. */
CJSON_PUBLIC(cJSON*)cJSON_AddNullToObject(cJSON *const object,const char*const name);
CJSON_PUBLIC(cJSON*)cJSON_AddTrueToObject(cJSON *const object,const char*const name);
CJSON_PUBLIC(cJSON*)cJSON_AddFalseToObject(cJSON *const object,const char*const name);
CJSON_PUBLIC(cJSON*)cJSON_AddBoolToObject(cJSON *const object,const char*const name,co
nst cJSON_bool boolean1); CJSON_PUBLIC(cJSON*)cJSON_AddNumberToObject(cJSON *const object,const char*const name,const double number); CJSON_PUBLIC(cJSON*)cJSON_AddStringToObject(cJSON *const object,const char*const name,const char*const string); CJSON_PUBLIC(cJSON*)cJSON_AddRawToObject(cJSON *const object,const char*const name,const char*const raw); CJSON_PUBLIC(cJSON*)cJSON_AddObjectToObject(cJSON *const object,const char*const name);
CJSON_PUBLIC(cJSON*)cJSON_AddArrayToObject(cJSON *const object,const char*const name);
/* Returns the number of items in an array (or object). */
extern int cJSON_GetArraySize(cJSON *array);//获取cjson对象数组成员的个数
/* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
extern cJSON *cJSON_GetArrayItem(cJSON *array,int item);//根据下标获取cjosn对象数组中的对象
/* Get item "string" from object. Case insensitive. */
extern cJSON *cJSON_GetObjectItem(cJSON *object,const char*string);//根据名称从对象中获取对应的 item 名称/值对
3.3 构造
3.3.1 创建对象
cJSON *jsonRespOut =NULL;
jsonRespOut =cJSON_CreateObject();/* 创建⼀个对象 */
if(!jsonRespOut)
{
cJSON_Delete(jsonRespOut);/* 创建对象失败之后删除对象 */
jsonRespOut =NULL;
return CMD_ERR;
}
3.3.2 往对象中添加名称/值对 - 数字
int opt =0x1234;
cJSON_AddNumberToObject(jsonRespOut,"opt", opt);/* 在对象中添加⼀个名称/值对,这个值是数字 */
3.3.3 往对象中添加名称/值对 - 字符串
char Value[]="1.2.0.0";
cJSON_AddStringToObject(jsonRespOut,"version",(char*)Value);
3.3.4 往对象中添加名称/值对 - 数组
int Value[2]={1,1}
cJSON *array =NULL;
array =cJSON_CreateArray();
if(!array)
return;
cJSON_AddItemToObject(jsonRespOut,"voiceMode", array);
cJSON_AddItemToArray(array,cJSON_CreateNumber((int)Value[0]));
cJSON_AddItemToArray(array,cJSON_CreateNumber((int)Value[1]));
3.4 解析
3.4.1 从给定的json字符串中得到cjson对象
char*jsonBuff ="{"opt":62011,"optNum":1,"voiceMode":[1,1],"version":"1.2.0.0.0"}";/* 存放JSON字符串数据 */ cJSON *jsonData =NULL;
jsonData =cJSON_Parse(jsonBuff);/* 从给定的json字符串中得到cjson对象 */
3.4.2 从cjson对象中根据名称得到 item 名称/值对
cJSON *item=NULL;
item =cJSON_GetObjectItem(jsonData,"opt");/* 根据名称 ("opt") 从对象 (jsonData) 中获取对应的名称/值对(item) */ int opt = item->valueint;
3.4.3 解析数字
cJSON *item=NULL;
item =cJSON_GetObjectItem(jsonData,"opt");/* 根据名称获取对应的值(cjson对象) */
int opt = item->valueint;
或者
/* 根据名称获取对应的值(cjson对象) */
int opt =0xFF;
if(cJSON_HasObjectItem(jsonData,"opt"))
{
cJSON_GetObjectItem(jsonData,"opt")->valueint;
}
3.4.4 解析字符串
/* 根据名称获取对应的值(cjson对象) */
char*version =cJSON_GetObjectItem(jsonData,"version")->valuestring; 3.4.5 解析数组
cJSON *item=NULL;
item =cJSON_GetObjectItem(jsonData,"voiceMode");
size =cJSON_GetArraySize(item);
if(size !=2)
return PARAMS_ERR;
else
{
sound_channel =cJSON_GetArrayItem(item,0)->valueint;/* 获取数组[0] */    voice_mode    =cJSON_GetArrayItem(item,1)->valueint;/* 获取数组[1] */ }

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。