当前位置:网站首页>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也是完全没问题的这个东西不难,就是要细心。
边栏推荐
- Redis source code and design analysis -- 16. AOF persistence mechanism
- 3DCAT v2.1.3新版本发布,这三大功能更新你不容错过!
- PageHelper还能结合Lambda表达式实现简洁的分页封装
- What are the advantages of real-time cloud rendering
- Hit the test site directly: summary of common agile knowledge points in PMP examination
- Drawing PDF tables (I) drawing excel table styles in PDF and downloading them through iText (supporting Chinese fonts)
- CVE-2022-33891 Apache spark shell 命令注入漏洞复现
- 2022/7/23
- WPF 实现用户头像选择器
- mysql case when
猜你喜欢

Redis source code and design analysis -- 16. AOF persistence mechanism

OSPF comprehensive experiment

Why the future of digitalization depends on 3D real-time rendering

Tme2022 campus recruitment background development / operation development / business operation and maintenance / application development written examination (I) a little self analysis of programming q

Lwip之内存与包缓冲管理

如何判断静态代码质量分析工具的性能?这五大因素必须考虑

Dating activity records

What are the advantages of real-time cloud rendering

Ch582 ble 5.0 uses Le coded broadcast and connection

CH582 BLE 5.0 使用 LE Coded 广播和连接
随机推荐
如何判断静态代码质量分析工具的性能?这五大因素必须考虑
3DCAT v2.1.3新版本发布,这三大功能更新你不容错过!
PageHelper can also be combined with lambda expressions to achieve concise paging encapsulation
mysql case when
2022/7/23
Redis源码与设计剖析 -- 18.Redis网络连接库分析
"Deprecated gradle features were used in this build, making it incompatible with gradle 6.0" problem solving
SDLC software development life cycle and model
OSPF---开放式最短优先路径协议
云VR:虚拟现实专业化的下一步
Lwip之内存与包缓冲管理
软件测试基础知识(思维导图)
Thesis reading_ Multi task learning_ MMoE
Good news! Ruiyun technology was awarded the member unit of 5g integrated application special committee of "sailing on the sea"
C LINQ de Duplication & de duplication sum
十九岁的总结
Principle and implementation of UDP penetration NAT in P2P
Sorting also needs to know the information and linked list
What is an IP SSL certificate and how to apply for it?
"Digital security" alert NFT's seven Scams