# cJSON 的使用
###### 简介
cJSON 是一个基于C语言的,可跨平台的解析字典、字符串组的数据结构。
###### 下载地址
https://github.com/JHiroGuo/cJson
在cJSON中,json对象可以是json、字符串、数组等。
###### 结构
```C
/* cJSON Types: */
#define cJSON_False 0
#define cJSON_True 1
#define cJSON_NULL 2
#define cJSON_Number 3
#define cJSON_String 4
#define cJSON_Array 5
#define cJSON_Object 6
#define cJSON_IsReference 256
/* The cJSON structure: */
typedef struct cJSON {
struct cJSON *next,*prev; /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
struct cJSON *child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
int type; /* The type of the item, as above. */
char *valuestring; /* The item's string, if type==cJSON_String */
int valueint; /* The item's number, if type==cJSON_Number */
double valuedouble; /* The item's number, if type==cJSON_Number */
char *string; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
} cJSON;
```
###### 解析Json
```C
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */
extern cJSON *cJSON_Parse(const char *value);
/* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */
extern char *cJSON_Print(cJSON *item);
/* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */
extern char *cJSON_PrintUnformatted(cJSON *item);
/* Delete a cJSON entity and all subentities. */
extern void cJSON_Delete(cJSON *c);
/* Returns the number of items in an array (or object). */
extern int cJSON_GetArraySize(cJSON *array);
/* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
extern cJSON *cJSON_GetArrayItem(cJSON *array,int item);
/* Get item "string" from object. Case insensitive. */
extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);
/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
extern const char *cJSON_GetErrorPtr(void);
```
###### 解析Demo
```c++
{
"semantic": {
"slots": {
"name": "张三"
}
},
"rc": 0,
"operation": "CALL",
"service": "telephone",
"text": "打电话给张三"
}
```
```c
#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"
int main()
{
char * jsonStr = "{\"semantic\":{\"slots\":{\"name\":\"张三\"}}, \"rc\":0, \"operation\":\"CALL\", \"service\":\"telephone\", \"text\":\"打电话给张三\"}";
cJSON * root = NULL;
cJSON * item = NULL;//cjson对象
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", "获取semantic下的cjson对象:");
item = cJSON_GetObjectItem(root, "semantic");
printf("%s\n", cJSON_Print(item));
printf("%s\n", "获取slots下的cjson对象");
item = cJSON_GetObjectItem(item, "slots");
printf("%s\n", cJSON_Print(item));
printf("%s\n", "获取name下的cjson对象");
item = cJSON_GetObjectItem(item, "name");
printf("%s\n", cJSON_Print(item));
printf("%s:", item->string); //打印Key
printf("%s\n", item->valuestring);//打印Value
}
return 0;
}
```
###### 构造函数
```C
/* These calls create a cJSON item of the appropriate type. */
extern cJSON *cJSON_CreateNull(void);
extern cJSON *cJSON_CreateTrue(void);
extern cJSON *cJSON_CreateFalse(void);
extern cJSON *cJSON_CreateBool(int b);
extern cJSON *cJSON_CreateNumber(double num);
extern cJSON *cJSON_CreateString(const char *string);
extern cJSON *cJSON_CreateArray(void);
extern cJSON *cJSON_CreateObject(void);
/* These utilities create an Array of count items. */
extern cJSON *cJSON_CreateIntArray(const int *numbers,int count);
extern cJSON *cJSON_CreateFloatArray(const float *numbers,int count);
extern cJSON *cJSON_CreateDoubleArray(const double *numbers,int count);
extern cJSON *cJSON_CreateStringArray(const char **strings,int count);
```
###### 解析Demo
```C
"semantic": {
"slots": {
"name": "张三"
}
},
"rc": 0,
"operation": "CALL",
"service": "telephone",
"text": "打电话给张三"
}
#include <stdio.h>
#include "cJSON.h"
int main()
{
cJSON * root = cJSON_CreateObject();
cJSON * item = cJSON_CreateObject();
cJSON * next = cJSON_CreateObject();
cJSON_AddItemToObject(root, "rc", cJSON_CreateNumber(0));//根节点下添加
cJSON_AddItemToObject(root, "operation", cJSON_CreateString("CALL"));
cJSON_AddItemToObject(root, "service", cJSON_CreateString("telephone"));
cJSON_AddItemToObject(root, "text", cJSON_CreateString("打电话给张三"));
cJSON_AddItemToObject(root, "semantic", item);//root节点下添加semantic节点
cJSON_AddItemToObject(item, "slots", next);//semantic节点下添加item节点
cJSON_AddItemToObject(next, "name", cJSON_CreateString("张三"));//添加name节点
printf("%s\n", cJSON_Print(root));
return 0;
}
```
###### 其他常用函数
```c
/* Update array items. */
extern void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem);
extern void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
/* Duplicate a cJSON item */
extern cJSON *cJSON_Duplicate(cJSON *item,int recurse);
/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will
need to be released. With recurse!=0, it will duplicate any children connected to the item.
The item->next and ->prev pointers are always zero on return from Duplicate. */
/* Delete a cJSON entity and all subentities. */
extern void cJSON_Delete(cJSON *c);
```
PS:
cJSON 对象是指针类型 ,所以在创建之后一定要调用 cJSON_Delete 删除函数。
```c
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "cJSON.h"
static void print_cjson_obj_value(const cJSON * obj)
{
if (!obj) {
printf("NULL object!\n");
}
switch (obj->type) {
case cJSON_False:
printf("%s: false\n", obj->string);
break;
case cJSON_True:
printf("%s: true\n", obj->string);
break;
case cJSON_NULL:
printf("%s: cJSON_NULL\n", obj->string);
break;
case cJSON_Number:
printf("%s: %d, %f\n",
obj->string, obj->valueint, obj->valuedouble);
break;
case cJSON_String:
printf("%s: %s\n",
obj->string, obj->valuestring);
break;
case cJSON_Array:
printf("%s: cJSON_Array\n", obj->string);
break;
case cJSON_Object:
printf("%s: cJSON_Object\n", obj->string);
break;
default:
printf("unknown type\n");
}
}
/* *
* 字符串转换成json对象
*/
int test_parse_json_string_to_object()
{
#define MY_JSON_STRING \
"{\"name\": \"Jack (\\\"Bee\\\") Nimble\", \
\"format\": { \
\"type\": \"rect\", \
\"width\": 1920, \
\"interlace\": true, \
\"height\": 123.1 \
},
没有合适的资源?快使用搜索试试~ 我知道了~
基于C语言 跨平台json解析.zip
共6个文件
h:3个
md:1个
cpp:1个
需积分: 5 0 下载量 55 浏览量
2024-03-15
16:42:38
上传
评论
收藏 17KB ZIP 举报
温馨提示
【项目资源】: 包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。 包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】: 所有源码都经过严格测试,可以直接运行。 功能在确认正常工作后才上传。 【适用人群】: 适用于希望学习不同技术领域的小白或进阶学习者。 可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】: 项目具有较高的学习借鉴价值,也可直接拿来修改复刻。 对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】: 有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 鼓励下载和使用,并欢迎大家互相学习,共同进步。 # 注意 1. 本资源仅用于开源学习和技术交流。不可商用等,一切后果由使用者承担。 2. 部分字体以及插图等来自网络,若是侵权请联系删除。
资源推荐
资源详情
资源评论
收起资源包目录
基于C语言 跨平台json解析.zip (6个子文件)
cJson-master
cJSON
cJSON.c 23KB
XcJSON.cpp 2KB
include_cJSON.h 389B
XcJSON.h 2KB
cJSON.h 7KB
README.md 19KB
共 6 条
- 1
资源评论
妄北y
- 粉丝: 1w+
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功