当前位置:网站首页>C语言 cJSON库的使用
C语言 cJSON库的使用
2022-07-25 18:00:00 【smile_5me】
首先先下载cJSON的库,还是很容易找到的下载的方式
解开压缩包,将里面的cJSON.h和cJSON.c放到你的工程中,就可以开始玩耍了
先上一个简单代码:
#include<stdio.h>
#include<stdlib.h>
#include"cJSON.h"
int main(void)
{
char *data = "{\"jjj\":[\"kkk\",\"lll\"]}";
//从缓冲区中解析出JSON结构
cJSON * json= cJSON_Parse(data);
//将传入的JSON结构转化为字符串 并打印
char *json_data = NULL;
printf("data:%s\n",json_data = cJSON_Print(json));
free(json_data);
//将JSON结构所占用的数据空间释放
cJSON_Delete(json);
return 0;
}输出结果:
data:{
"jjj": ["kkk", "lll"]
}
然后可以进阶一下,代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"cJSON.h"
int main(void)
{
//创建一个空的文档(对象)
cJSON *json = cJSON_CreateObject();
cJSON_AddItemToObject(json,"name",cJSON_CreateString("马保国"));
//向文档中添加一个键值对
//cJSON_AddItemToObject(json,"age",cJSON_CreateNumber(29));
cJSON_AddNumberToObject(json,"年龄",69);
cJSON *array = NULL;
cJSON_AddItemToObject(json,"技能",array=cJSON_CreateArray());
cJSON_AddItemToArray(array,cJSON_CreateString("耗子尾汁"));
cJSON_AddItemToArray(array,cJSON_CreateString("闪电鞭"));
cJSON_AddItemToArray(array,cJSON_CreateString("不讲武得"));
cJSON_AddNumberToObject(json,"score",0);
cJSON_AddStringToObject(json,"address","xxx");
//将json结构格式化到缓冲区
char *buf = cJSON_Print(json);
//打开文件写入json内容
FILE *fp = fopen("create.json","w");
fwrite(buf,strlen(buf),1,fp);
free(buf);
fclose(fp);
//释放json结构所占用的内存
cJSON_Delete(json);
return 0;
}输出结果(写入文件里):
{
"name": "马保国",
"年龄": 69,
"技能": ["耗子尾汁", "闪电鞭", "不讲武得"],
"score": 0,
"address": "xxx"
}然后再再进阶一次:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"cJSON.h"
int main(void)
{
//先创建空对象
cJSON *json = cJSON_CreateObject();
//在对象上添加键值对
cJSON_AddStringToObject(json,"country","china");
//添加数组
cJSON *array = NULL;
cJSON_AddItemToObject(json,"stars",array=cJSON_CreateArray());
//在数组上添加对象
cJSON *obj = NULL;
cJSON_AddItemToArray(array,obj=cJSON_CreateObject());
cJSON_AddItemToObject(obj,"name",cJSON_CreateString("Faye"));
cJSON_AddStringToObject(obj,"address","beijing");
//在对象上添加键值对
cJSON_AddItemToArray(array,obj=cJSON_CreateObject());
cJSON_AddItemToObject(obj,"name",cJSON_CreateString("andy"));
cJSON_AddItemToObject(obj,"address",cJSON_CreateString("HK"));
cJSON_AddItemToArray(array,obj=cJSON_CreateObject());
cJSON_AddStringToObject(obj,"name","eddie");
cJSON_AddStringToObject(obj,"address","TaiWan");
//清理工作
FILE *fp = fopen("create.json","w");
char *buf = cJSON_Print(json);
fwrite(buf,strlen(buf),1,fp);
fclose(fp);
cJSON_Delete(json);
return 0;
}输出结果(写到文件里):
{
"country": "china",
"stars": [{
"name": "Faye",
"address": "beijing"
}, {
"name": "andy",
"address": "HK"
}, {
"name": "eddie",
"address": "TaiWan"
}]
}编译:
gcc json_example.c cJSON.c -o json_example -L . -lm相信看完这几个例子,写那种复杂一点的json也是完全没问题的这个东西不难,就是要细心。
边栏推荐
- Wu Enda's machine learning programming operation cannot be suspended pause problem solved
- “Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0”问题解决
- Redistemplate solves the problem of oversold inventory in the seckill system with high speed - redis transaction + optimistic lock mechanism
- Go defer and recover simple notes
- "Digital security" alert NFT's seven Scams
- Installation steps and usage of NVM under windows10 system
- 带你初步了解多方安全计算(MPC)
- Briefly describe synchronized and lock upgrade
- Good news! Ruiyun technology was awarded the member unit of 5g integrated application special committee of "sailing on the sea"
- 实时云渲染有哪些优势
猜你喜欢

Good news! Ruiyun technology was awarded the member unit of 5g integrated application special committee of "sailing on the sea"

带你初步了解多方安全计算(MPC)

CH582 BLE 5.0 使用 LE Coded 广播和连接

Highlights

Oracle导入出错:IMP-00038: 无法转换为环境字符集句柄

Auditing相关注解

实时云渲染有哪些优势

Sorting also needs to know the information and linked list

Installation steps and usage of NVM under windows10 system

3DCAT v2.1.3新版本发布,这三大功能更新你不容错过!
随机推荐
[MySQL]数据库中的索引为什么是用B+树来实现? 哈希表/红黑树/B树是否可行呢?
Redis source code and design analysis -- 16. AOF persistence mechanism
H5测试点(思维导图)
为什么数字化未来取决于3D实时渲染
The new version of 3dcat v2.1.3 has been released. You can't miss these three function updates!
ROS learning notes (IV) ROS cannot solve rosdep init or update
go defer与recover简单笔记
Notes on Flickr's dataset
RedisTemplate解决高并发下秒杀系统库存超卖方案 — Redis事务+乐观锁机制
Auditing相关注解
Why the future of digitalization depends on 3D real-time rendering
Landmark buildings around the world
mongodb 集群及分片
Cross validation (CV) learning notes
Mock服务moco系列(二)- Json格式、File文件、Header、Cookie、解决中文乱码
Dating activity records
SDLC software development life cycle and model
Go defer and recover simple notes
Mongodb cluster and sharding
How many points did NPDP pass? How to pass with high scores?