cJSON 解析

https://github.com/DaveGamble/cJSON

https://blog.csdn.net/bjbz_cxy/article/details/79173603

1. 介绍

/* 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;
 /* The item's number, if type==cJSON_Number */
 double valuedouble;
​
 /* 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;

2. 使用

2.1. 基础函数

  • cJSON_Parse
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);

作用:将一个JSON数据包,按照cJSON结构体的结构序列化,并在堆中开辟一块内存存储cJSON结构体
返回值:成功返回一个指向内存块中的cJSON的指针,失败返回NULL-

  • cJSON_GetObjectItem
/* Get item "string" from object. Case insensitive. */
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string);

作用:获取JSON字符串字段值
返回值:成功返回一个指向cJSON类型的结构体指针,失败返回NULL

  • cJSON_GetArraySize
/* Returns the number of items in an array (or object). */
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);

作用:获取数组成员对象个数
返回值:数组成员对象个数

  • cJSON_Print
/* Render a cJSON entity to text for transfer/storage. */
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);

作用:将cJSON数据解析成JSON字符串,并在堆中开辟一块char的内存空间存储JSON字符串
返回值:成功返回一个char
指针该指针指向位于堆中JSON字符串,失败返回NULL

  • cJSON_Delete
/* Delete a cJSON entity and all subentities. */
CJSON_PUBLIC(void) cJSON_Delete(cJSON *c);

作用:释放位于堆中cJSON结构体内存
返回值:无

2.2. 简单使用

#include 
#include "cJSON.h"
​
​
int main()
{
 char* json_string = \
 // 用char* 模拟一个JSON字符串
 "{"\
 "test_1": "0"\
 "test_2": "1"\
 "test_3": "2"\
 "}";

 // JSON字符串到cJSON格式
 cJSON* root = cJSON_Parse(json_string);
 // 判断是否解析成功
 if (!root)
 {
 printf("json pack into cjson error...");
 }
 else
 {
 cJSON_Print(root);
 }
​
 // 获取字段值
 char* test_1_string = cJSON_GetObjectltem(root, "test_1")->valuestring;
 char* test_2_string = cJSON_GetObjectltem(root, "test_2")->valuestring;
 char* test_3_string = cJSON_GetObjectltem(root, "test_3")->valuestring;
​
 // 打印输出
 printf("%s", *test_1_string);
 printf("%s", *test_2_string);
 printf("%s", *test_3_string);
​
 // delete cjson
 cJSON_Delete(root);
}

2.3. 解析数组

#include 
#include "cJSON.h"
​
​
int main()
{
 char* json_string = \
 // 用char* 模拟一个JSON字符串
 "{ ", \
 "test_1":"1", \
 "test_2":"2", \
 "test_arr":"[{", \
 "{", \
 "test_1":"arr_1", \
 "test_2":"arr_2", \
 "test_3":"arr_3", \
 "}", \
 "{"\
 "test_1":"1", \
 "test_2":"2", \
 "test_3":"3", \
 "}", \
 "}]", \
 "test_3" :"3", \
 "}"//json_arr string;
​
 // 解析JSON字符串
 cJSON* root = cJSON_Parse(json_string);
​
 // 判断是否解析成功
 if (!root)
 {
 printf("cjson error…")
 }
 else
 {
 cJSON_Print(root);
 }
​
 // 获取数组对象
 cJSON* test_arr = cJSON_GetObjectItem(root, "test_arr");
​
 // 获取数组对象个数
 int arr_size = cJSON_GetArraySize(test_arr);
​
 // 获取test_arr数组对象孩子节点
 cJSON* arr_item = test_arr->child;
​
 // 循环获取数组下每个字段的值并使用cJSON_Print打印
 for (int i = 0; i < arr_size; ++i)
 {
 cJSON_Print(cJSON_GetObjectItem(arr_item, "test_1"));
 cJSON_Print(cJSON_GetObjectItem(arr_item, "test_2"));
 cJSON_Print(cJSON_GetObjectItem(arr_item, "test_3"));
 arr_item = arr_item->next;//下一个子对象
 }
​
 // delete cjson
 cJSON_Delete(root);
}

你可能感兴趣的:(cJSON 解析)