cJSON 入门与应用
1. cJSON简介
cJSON aims to be the dumbest possible parser that you can get your job done with. It's a single file of C, and a single header file.
JSON is described best here: http://www.json.org/ It's like XML, but fat-free. You use it to move data around, store things, or just generally represent your program's state.
cJSON 下载地址:https://github.com/DaveGamble/cJSON
2. cJSON常用函数简介
cJSON * cJSON_CreateObject();
创建一个json对象,返回一个cJSON结构体类型的指针。
cJSON *cJSON_CreateArray();
创建一个数组对象,返回一个cJSON结构体类型的指针。
cJSON *cJSON_CreateString(const char *string);
创建一个字符串对象,传入一个char *类型的字符串,返回一个cJSON结构体类型的指针。
void cJSON_AddItemToArray(cJSON *array, cJSON *item);
向数组对象中添加一个元素,传入参数array为cJSON *结构体类型的指针,为数组对象; item为添加入数字对象中的对象指针。
void
cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item);
向json对象