Cjson 库使用

1. JSON简介

        JSON全称 JavaScript Object Notation,即 JS对象简谱,是一种轻量级的数据格式。
它采用完全独立于编程语言的文本格式来存储和表示数据,语法简洁、层次结构清晰,易于人阅读和编写,同时也易于机器解析和生成,有效的提升了网络传输效率。

2. CJSON的安装

下载安装链接 :https://files.cnblogs.com/files/txwtech/cJSONFiles.zip

下载后解压压缩包,只需要将cJSON.c 和 cJSON.h 文件和自己的工程一起编译即可。

3. JSON语法

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

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

对于键值,可以有如下值:

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

4. cJSON常用函数

4.1. 构造json常用的函数

CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void);	//创建对象---常用
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void);	//创建数组---常用
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count);//创建整型数组
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count);//创建双浮点型数组
CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name);//在对象中添加null
CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name);//在对象中添加true
CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name);//在对象中添加false
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_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);	//在对象中添加项目
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item);//在数组中添加项目

CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);//JSON数据结构转换为JSON字符串---有格式
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item);	//JSON数据结构转换为JSON字符串---无格式

CJSON_PUBLIC(void) cJSON_Delete(cJSON *item); //清除结构体

4.2. 解析json常用的函数

cJSON *cJSON_Parse(const char *value);
/*作用:将一个JSON数据包,按照cJSON结构体的结构序列化整个数据包,并在堆中开辟一块内存存储cJSON结构体
返回值:成功返回一个指向内存块中的cJSON的指针,失败返回NULL*/

cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);
/*作用:获取JSON字符串字段值
返回值:成功返回一个指向cJSON类型的结构体指针,失败返回NULL*/

int cJSON_GetArraySize(cJSON *array);
/*作用:获取数组成员对象个数
返回值:数组成员对象个数*/

void  cJSON_Delete(cJSON *c);
/*作用:释放位于堆中cJSON结构体内存
返回值:无*/

5. cJSON使用

5.1. 构建json格式数据

{
  "name": "Jason",
  "age": 39,
  "height": 1.92,
  "gender": "M",
  "salary": 70000,
  "married": true,
  "skill": ["c", "Java", "Python"],
  "address": 
    {
        "country": "China",
        "zip-code": 11111
    }
}
#include <stdio.h>
#include "cJSON.h"

int main() {	
	cJSON* cjson = NULL;
    cJSON* cjson_skill = NULL;
    cJSON* cjson_address = NULL;

	cjson = cJSON_CreateObject();

    cJSON_AddStringToObject(cjson, "name", "jason");
    cJSON_AddNumberToObject(cjson, "age", 39);
    cJSON_AddStringToObject(cjson, "gender", "M");
    cJSON_AddNumberToObject(cjson, "salary", 70000);
    cJSON_AddTrueToObject(cjson, "married");

    cjson_skill = cJSON_CreateArray();
    cJSON_AddItemToArray(cjson_skill, cJSON_CreateString("C"));
    cJSON_AddItemToArray(cjson_skill, cJSON_CreateString("Java"));
    cJSON_AddItemToArray(cjson_skill, cJSON_CreateString("Python"));
	cJSON_AddItemToObject(cjson, "skill", cjson_skill);

    cjson_address = cJSON_CreateObject();
    cJSON_AddStringToObject(cjson_address, "country", "China");
    cJSON_AddNumberToObject(cjson_address, "zip-code", 111111);
    cJSON_AddItemToObject(cjson, "skill", cjson_address);

    char *str = cJSON_Print(cjson);
	printf("%s\n", str);
    
    return 0;
}
{
        "name": "jason",
        "age":  39,
        "gender":       "M",
        "salary":       70000,
        "married":      true,
        "skill":        ["C", "Java", "Python"],
        "skill":        {
                "country":      "China",
                "zip-code":     111111
        }
}

5.2. 解析json格式数据

5.2.1. 解析键值对

{
  "name": "Jason",
  "age": 39,
  "height": 1.92,
  "gender": "M",
  "salary": 70000,
  "married": true,
}
#include <stdio.h>
#include "cJSON.h"

int main() {

char strbuf[] = "{\"name\": \"Jason\",\"age\": 39,\"height\": 1.92,\"gender\": \"M\",\"salary\": 70000,\"married\": true}";

    cJSON* cjson = NULL;
    cJSON* tmp = NULL;
    cjson = cJSON_Parse(strbuf);
    tmp = cJSON_GetObjectItem(cjson, "name");
    printf("name : %s\n", tmp->valuestring);
    
    tmp = cJSON_GetObjectItem(cjson, "age");
    printf("age : %d\n", tmp->valueint);

    tmp = cJSON_GetObjectItem(cjson, "height");
    printf("height : %f\n", tmp->valuedouble);

    tmp = cJSON_GetObjectItem(cjson, "gender");
    printf("gender : %s\n", tmp->valuestring);

    tmp = cJSON_GetObjectItem(cjson, "salary");
    printf("salary : %d\n", tmp->valueint);

    tmp = cJSON_GetObjectItem(cjson, "married");
    printf("married : %d\n", tmp->valueint);

    cJSON_Delete(cjson);

    return 0;
}
name : Jason
age : 39
height : 1.920000
gender : M
salary : 70000
married : 1

5.2.2. 解析数组

{
  "name": "Jason",
  "age": 39,
  "height": 1.92,
  "gender": "M",
  "salary": 70000,
  "married": true,
  "skill": ["c", "Java", "Python"]
}
#include <stdio.h>
#include "cJSON.h"

int main() {

char strbuf[] = "{\"name\": \"Jason\",\"age\": 39,\"height\": 1.92,\"gender\": \"M\",\"salary\": 70000,\"married\": true, \"skill\": [\"c\", \"Java\", \"Python\"]}";
    
    int skill_array_size, i = 0;
    cJSON* cjson = NULL;
    cJSON* cjson_skill = NULL;
    cJSON* skill_item = NULL;
    cjson = cJSON_Parse(strbuf);
    
    cjson_skill = cJSON_GetObjectItem(cjson, "skill");
    skill_array_size = cJSON_GetArraySize(cjson_skill);
    
    for (i = 0; i < skill_array_size; i++) {
        skill_item = cJSON_GetArrayItem(cjson_skill, i);
        printf("%s\n", skill_item->valuestring);
    }

    cJSON_Delete(cjson);

    return 0;
}
c
Java
Python

5.2.3. 解析json嵌套数据

{
  "name": "Jason",
  "age": 39,
  "height": 1.92,
  "gender": "M",
  "salary": 70000,
  "married": true,
  "address": 
    {
        "country": "China",
        "zip-code": 11111
    }
}
#include <stdio.h>
#include "cJSON.h"

int main() {

char strbuf[] = "{\"name\": \"Jason\",\"age\": 39, \"address\": {\"country\": \"China\", \"zip-code\": 11111}}";
    
    int skill_array_size, i = 0;
    cJSON* cjson = NULL;
    cJSON* cjson_address = NULL;
    cJSON* address_item = NULL;
    cjson = cJSON_Parse(strbuf);
    
	cjson_address = cJSON_GetObjectItem(cjson, "address");
    
    address_item = cJSON_GetObjectItem(cjson_address, "country");
	printf("%s\n", address_item->valuestring);

    address_item = cJSON_GetObjectItem(cjson_address, "zip-code");
    printf("%d\n", address_item->valueint);
    
    cJSON_Delete(cjson);

    return 0;
}
China
11111
  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值