cJSON详解及其C函数接口使用

目录

JSON简介

JSON语法

CJSON介绍

cJSON源码

cJSON数据结构与常用接口

数据结构定义

常用接口

cJSON_Parse

cJSON_Delete

cJSON_Print

cJSON_Version

cJSON_GetArrayItem

与cJSON_GetObjectItem()类似的接口:

类型判断接口

创建cJSON对象接口

添加cJSON对象到链表

 从cJSON对象中删除一个cJSON对象

创建并添加到链表

使用实例

解析数据实例

解析数据实例一

实例运行结果

解析数据实例二

实例运行结果 

添加数据实例

实例

运行结果:


  • JSON简介

JSON:JavaScript对象表示法(JavaScript Object Notation)。是一种轻量级的数据交换格式。它基于ECMAScript的一个子集。JSON采用完全独立于语言的文本格式,但是也使用了类似C语音家族的习惯(包括C、C++、C#、Java、JavaScript、Perl、Python等)。这些特性使JSON成为理想的数据交换语言。易于人阅读和编写,同时也易于机器解析和生成(一般用于提升网络传输速率)。

JSON解析器和JSON库支持许多不同的编程语言。JSON文本格式在语法上与创建JavaScript对象的代码相同。由于这种相似性,无需解释器,JavaScript程序能够使用内建的eval()函数,用JSON数据来生成原生的JavaScript对象。

JSON是存储和交换文本信息的语法,类似XML。但JSON比XML更小、更快、更易解析;且JSON具有自我描述性、语法简单,易于理解。

  • JSON语法

  • JSON语法是JavaScript对象表示法语法的子集:

  1. 数据在键/值对中;

  2. 数据由逗号分隔;

  3. 花括号保存对象,也称一个文档对象;

  4. 方括号保存数组,每个数组成员用逗号隔开,并且每个数组成员可以是文档对象或者数组或者键值对。

  • JSON的三种语法:

  1. 键/值对key:value,用半角冒号分割。比如"name":"Faye"。

  2. 文档对象JSON对象写在花括号中,可以包含多个键/值对。比如{"name":"Ami", "address":"上海"}。

  3. 数组JSON数组在方括号中书写:数组成员可以是对象、值,也可以是数组(只要有意义)。比如{"love": ["c","c++","python","java"]}。

JSON的值可以是:数字(整数或浮点数)、字符串(在双引号中)、逻辑值(true或false)、数组(在方括号中)、对象(在花括号中)、null。

  • JSON两种结构:

  1. 对象:对象在js中表示为“{}”括起来的内容,数据结构为{key:value,key:value,...}的键值对的结构,在面向对象的语言中,key为对象的属性,value为对应的属性值,所以很容易理解,取值方法为 对象.key 获取属性值;

  2. 数组:数组在js中是中括号“[]”括起来的内容,数据结构为["java","javascript","vb",...],取值方式和所有语言中一样,使用索引获取。 

经过对象、数组2种结构就可以组合成复杂的数据结构了。

  • CJSON介绍

cJSON是一个仅有一个.h文件,一个.c文件组成的JSON解析器,它是由纯C(ANSI C89)实现的,跨平台性较好。cJSON是采用链表存储的。

cJSON库在使用的时候只需要如下两步:将cJSON.c(或者库文件)和cJSON.h添加到项目中即可;如果在命令行中进行链接还需要加上-lm表示链接math库。

  • cJSON源码

git clone https://github.com/tjcshx/cJson.git
  • cJSON数据结构与常用接口

整体来讲,cJSON库的数据结构和函数命名接口非常规范,基本上一看就懂,非常容易理解。

  • 数据结构定义

/* cJSON Types: */
#define cJSON_Invalid (0)
#define cJSON_False  (1 << 0)
#define cJSON_True   (1 << 1)
#define cJSON_NULL   (1 << 2)
#define cJSON_Number (1 << 3)
#define cJSON_String (1 << 4)
#define cJSON_Array  (1 << 5)
#define cJSON_Object (1 << 6)
#define cJSON_Raw    (1 << 7) /* raw json */

#define cJSON_IsReference 256
#define cJSON_StringIsConst 512

/* 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;

next、prev用于遍历数组或对象链的前向后向链表指针;child指向数组或对象的孩子节点;type是value的类型;valuestring是字符串值;valueint是整数值;valuedouble是浮点数值;string是key的名字。还是非常容易理解的。

  • 常用接口

  • 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_Delete

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

    作用:释放位于堆中cJSON结构体内存。

    返回值:无

    注意:在使用cJSON_Parse()获取cJSON指针后,若不再使用了,则需要调用cJSON_Delete()对其释放,否则会导致内存泄漏。

  • cJSON_Print

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

    作用:将cJSON数据解析成JSON字符串,并在堆中开辟一块char*的内存空间存储JSON字符串。cJSON_PrintUnformatted()与cJSON_Print()类似,只是打印输出不带格式,而只是一个字符串数据。

    返回值:成功返回一个char*指针该指针指向位于堆中JSON字符串,失败返回NULL。

    注意:通过cJSON_Print()可以将cJSON链表中所有的cJSON对象打印出来,但是需要手动去释放对应的资源:free(char *)。

  • cJSON_Version

    /* returns the version of cJSON as a string */
    CJSON_PUBLIC(const char*) cJSON_Version(void);

    作用:获取当前使用的cJSON库的版本号。

    返回值:返回一个字符串数据。

  • cJSON_GetArrayItem

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

    作用:从object的cJSON链中寻找key为string的cJSON对象。

    返回值:成功返回一个指向cJSON类型的结构体指针,失败返回NULL。

  •  

    与cJSON_GetObjectItem()类似的接口:

     

    * Returns the number of items in an array (or object). */
    CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);
    /* Retrieve item number "index" from array "array". Returns NULL if unsuccessful. */
    CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index);
    CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string);
    CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string);
    /* Check if the item is a string and return its valuestring */
    CJSON_PUBLIC(char *) cJSON_GetStringValue(cJSON *item);

     

  • 类型判断接口

下面的接口用于判断具体cJSON指针指向的item类型:

/* These functions check the type of an item */
CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item);
  • 创建cJSON对象接口

下面的接口用于创建指定类型的cJSON数据结构对象:

/* raw json */
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw);
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void);
/* These calls create a cJSON item of the appropriate type. */
CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean);
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num);
CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string);
/* These utilities create an Array of count items. */
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char **strings, int count);
  • 添加cJSON对象到链表

下面的接口用于将创建的cJSON对象添加到链表中:

/* 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);
  •  从cJSON对象中删除一个cJSON对象

下面的接口用于从现有的cJSON链表中删除一个对象:

/* Remove/Detatch items from Arrays/Objects. */
CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which);
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string);
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string);
CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string);

 

  • 创建并添加到链表

下面的接口用于创建并添加cJSON对象到指定的链表(简化操作):

/* 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);
  • 使用实例

  • 解析数据实例

  • 解析数据实例一

只有一层键值,即无嵌套键值。

#include <stdio.h>
#include <string.h>
#include "cJSON.h"

int main (int argc, char **argv)
{
    char resolve_buf[] =  "{\"method\":\"thing.service.property.set\",\"id\":\"412505849\",      
\"LightSwitch\":1,\"version\":\"1.0.0\"}";
    cJSON *json = NULL;
    cJSON *json_val = NULL;
    cJSON *json_timestamp = NULL;

    printf("%s\n", resolve_buf);
    json = cJSON_Parse(resolve_buf);
    if (NULL == json)
    {   
        printf("cJSON_Parse error:%s\n", cJSON_GetErrorPtr());
    }   

    json_val = cJSON_GetObjectItem(json, "LightSwitch");
    if (cJSON_Number == json_val->type)
    {   
        printf("LightSwitch:%d\n", json_val->valueint);
    }   

    cJSON_Delete(json);
    return 0;
} /* ----- End of main() ----- */
  • 实例运行结果

2020072911202430.png

  • 解析数据实例二

有多层键值,即有嵌套键值。例如:

{
        "method":       "thing.service.property.set",
        "id":   "412505849",
        "params":       {
                "LightSwitch":  1
        },
        "version":      "1.0.0"
}
#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"

void printJson(cJSON * root)//以递归的方式打印json的最内层键值对
{
    int i = 0;
    for(; i<cJSON_GetArraySize(root); i++)   //遍历最外层json键值对
    {   
        cJSON * item = cJSON_GetArrayItem(root, i);    
        if(cJSON_Object == item->type)      //如果对应键的值仍为cJSON_Object就递归调用printJson
            printJson(item);
        else                                //值不为json对象就直接打印出键和值
        {   
            printf("%s->", item->string);
            printf("%s\n", cJSON_Print(item));
        }       
    }       
}

int main()
{
    char * jsonStr = "{\"method\":\"thing.service.property.set\",\"id\":\"412505849\",\"params\":
{\"LightSwitch\":1},\"version\":\"1.0.0\"}";
    cJSON * root = NULL;
    cJSON * item = NULL;

    root = cJSON_Parse(jsonStr);
    if (!root) 
    {   
        printf("Error before: [%s]\n",cJSON_GetErrorPtr());
    }
    else
    {
        printf("%s\n", "有格式的方式打印Json:");
        printf("%s\n\n", cJSON_Print(root));
        printf("%s\n", "无格式方式打印json:");
        printf("%s\n\n", cJSON_PrintUnformatted(root));

        printf("%s\n", "一步一步的获取name 键值对:");
        printf("%s\n", "获取params下的cjson对象:");
        item = cJSON_GetObjectItem(root, "params");
        printf("%s\n", cJSON_Print(item));
        printf("%s\n", "获取params下的cjson对象");
        item = cJSON_GetObjectItem(item, "LightSwitch");
        printf("%s\n", cJSON_Print(item));


        printf("\n%s\n", "打印json所有最内层键值对:");
        printJson(root);
    }
    return 0;
}
  • 实例运行结果 

20200729112702975.png

  • 添加数据实例

  • 实例

添加具有内部嵌套键值的。

#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"
int main(int argc, char **argv)
{
    char *cjson_str = NULL;
    cJSON * root =  cJSON_CreateObject();
    cJSON * item =  cJSON_CreateObject();
    cJSON * next =  cJSON_CreateObject();

    cJSON_AddItemToObject(root, "root", cJSON_CreateNumber(0));//根节点下添加
    cJSON_AddItemToObject(root, "method", cJSON_CreateString("thing.service.property.set"));
    cJSON_AddItemToObject(root, "id", cJSON_CreateString("412505849"));
    cJSON_AddItemToObject(root, "params", item); //添加嵌套键值
    cJSON_AddItemToObject(item, "LightSwitch", cJSON_CreateNumber(1));//内部嵌套键值                 
root节点下添加semantic节点

    cJSON_AddItemToObject(root, "version", cJSON_CreateString("1.0.0"));

    cjson_str = cJSON_Print(root);
    printf("first json:\n%s\n", cjson_str);
    free(cjson_str);

    cJSON_AddStringToObject(next, "number", "13423452334");
    cJSON_AddNumberToObject(next, "age", 35);
    cJSON_AddBoolToObject(next, "man", 1); 

    cjson_str = cJSON_Print(root);
    printf("second json:\n%s\n", cjson_str);
    free(cjson_str);

    cJSON_Delete(root);

    return 0;
}
  • 运行结果:

20200729125407589.png

 

 

  • 18
    点赞
  • 104
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
cJSON是一个轻量级的JSON解析器和生成器,它可以在C语言中使用。cJSON提供了一种方便的方式来解析和生成JSON数据,使得在C语言中处理JSON数据变得更加简单。以下是cJSON的一些常见用法。 ## 解析JSON数据 首先需要将JSON字符串转换成cJSON对象,然后根据需要获取对象的值。 ```c char* json_str = "{\"name\":\"Tom\",\"age\":20,\"gender\":\"male\"}"; cJSON* json = cJSON_Parse(json_str); if (json != NULL) { cJSON* name = cJSON_GetObjectItem(json, "name"); if (cJSON_IsString(name) && (name->valuestring != NULL)) { printf("name: %s\n", name->valuestring); } cJSON* age = cJSON_GetObjectItem(json, "age"); if (cJSON_IsNumber(age)) { printf("age: %d\n", age->valueint); } cJSON* gender = cJSON_GetObjectItem(json, "gender"); if (cJSON_IsString(gender) && (gender->valuestring != NULL)) { printf("gender: %s\n", gender->valuestring); } cJSON_Delete(json); } ``` 在上面的代码中,我们首先定义了一个JSON字符串,并使用`cJSON_Parse()`函数将其转换成cJSON对象。然后,我们使用`cJSON_GetObjectItem()`函数获取对象的值,该函数需要传入要获取的值的key。最后,我们使用`cJSON_IsString()`和`cJSON_IsNumber()`函数来检查获取到的值是否为字符串或数字类型。 ## 生成JSON数据 我们可以使用cJSON来生成JSON数据,并将其转换成JSON字符串。 ```c cJSON* json = cJSON_CreateObject(); if (json != NULL) { cJSON_AddStringToObject(json, "name", "Tom"); cJSON_AddNumberToObject(json, "age", 20); cJSON_AddStringToObject(json, "gender", "male"); char* json_str = cJSON_Print(json); printf("json_str: %s\n", json_str); free(json_str); cJSON_Delete(json); } ``` 在上面的代码中,我们首先使用`cJSON_CreateObject()`函数创建一个cJSON对象。然后,我们使用`cJSON_AddStringToObject()`和`cJSON_AddNumberToObject()`函数向对象中添加值。最后,我们使用`cJSON_Print()`函数将对象转换成JSON字符串,并使用`free()`函数释放内存。需要注意的是,在使用完cJSON对象后,需要使用`cJSON_Delete()`函数将其删除以释放内存。 ## 数组操作 cJSON还支持数组操作。 ```c cJSON* json = cJSON_CreateArray(); if (json != NULL) { cJSON* name1 = cJSON_CreateString("Tom"); cJSON_AddItemToArray(json, name1); cJSON* name2 = cJSON_CreateString("Mary"); cJSON_AddItemToArray(json, name2); char* json_str = cJSON_Print(json); printf("json_str: %s\n", json_str); free(json_str); cJSON_Delete(json); } ``` 在上面的代码中,我们首先使用`cJSON_CreateArray()`函数创建一个cJSON数组对象。然后,我们使用`cJSON_CreateString()`函数创建两个字符串对象,并使用`cJSON_AddItemToArray()`函数将它们添加到数组中。最后,我们使用`cJSON_Print()`函数将对象转换成JSON字符串,并使用`free()`函数释放内存。 以上是cJSON的一些常见用法,可以帮助我们在C语言中更加方便地处理JSON数据。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XiaoCheng'Blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值