Skip to content

cJSON

cJSON使用详细教程 | 一个轻量级C语言JSON解析器-CSDN博客

json语法

JSON对象是一个无序的"名称/值"键值对的集合:

  • 以"{“开始,以”}"结束,允许嵌套使用;
  • 每个名称和值成对出现,名称和值之间使用":"分隔;
  • 键值对之间用","分隔
  • 在这些字符前后允许存在无意义的空白符;

这里面可以有

  • 一个新的json对象
  • 数组:使用"[“和”]"表示
  • 数字:直接表示,可以是整数,也可以是浮点数
  • 字符串:使用引号"表示
  • 字面值:false、null、true中的一个(必须是小写)

https://github.com/DaveGamble/cJSON

从这里面获取的实际的源码文件只有两个cJSON.c/h

数据类型

cjson里面使用一个结构体表示一个json数据

c
/* The cJSON structure: */
typedef struct cJSON
{
    /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem 链表记录前后的JSON数据*/
    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. 
    子对象是一个新的JSON数据*/
    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;

创建

cJSON使用的一个链表的形式进行建立的

  • 头指针:指向链表头结点的指针;
  • 头结点:不存放有效数据,方便链表操作;
  • 首节点:第一个存放有效数据的节点;
  • 尾节点:最后一个存放有效数据的节点;
  1. 创建头指针
c
 cJSON* cjson_test = NULL;
  1. 建立头结点
c
cjson_test = cJSON_CreateObject();
  1. 添加信息
c
/* 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, const cJSON_bool boolean);
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);
c
int main(void){
    cJSON *root = cJSON_CreateObject();
    cJSON *message = cJSON_CreateObject();
    cJSON_AddStringToObject(root, "name", "John");
    cJSON_AddNumberToObject(root, "age", 25);
    cJSON_AddStringToObject(message, "city", "New York");
    cJSON_AddItemToObject(root, "address", message);
    char * data = cJSON_Print(root);  //获取字符形式
    printf(data);

}
c
cJSON_AddItemToObject(root, "language", cJSON_CreateString( "C" ));

这一个实际和cJSON_AddStringToObject(root, "language", "C");是一样的

数据解析

  1. 创建一个链表头部
c
cJSON *cjson = NULL;
  1. 解析数据
c
(cJSON *)cJSON_Parse(const char *value);
  1. 获取对应的值
c
(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string)
  1. 解析数组
c
(int) cJSON_GetArraySize(const cJSON *array);
(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index);
  1. 释放内存
c
(void) cJSON_Delete(cJSON *item);

这一个系列操作会使用大量内存, 需要及时释放内存