当前位置:网站首页>Use of C language cjson Library
Use of C language cjson Library
2022-07-25 18:05:00 【smile_ 5me】
Download first cJSON The library of , It's still easy to find a way to download
Unpacking a compressed package , The inside cJSON.h and cJSON.c Put it into your project , You can start playing
Let's start with a simple code :
#include<stdio.h>
#include<stdlib.h>
#include"cJSON.h"
int main(void)
{
char *data = "{\"jjj\":[\"kkk\",\"lll\"]}";
// Parse from buffer JSON structure
cJSON * json= cJSON_Parse(data);
// Will the incoming JSON Structure into a string And print
char *json_data = NULL;
printf("data:%s\n",json_data = cJSON_Print(json));
free(json_data);
// take JSON The data space occupied by the structure is released
cJSON_Delete(json);
return 0;
}Output results :
data:{
"jjj": ["kkk", "lll"]
}
Then you can advance , The code is as follows :
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"cJSON.h"
int main(void)
{
// Create an empty document ( object )
cJSON *json = cJSON_CreateObject();
cJSON_AddItemToObject(json,"name",cJSON_CreateString(" Ma Baoguo "));
// Add a key value pair to the document
//cJSON_AddItemToObject(json,"age",cJSON_CreateNumber(29));
cJSON_AddNumberToObject(json," Age ",69);
cJSON *array = NULL;
cJSON_AddItemToObject(json," Skill ",array=cJSON_CreateArray());
cJSON_AddItemToArray(array,cJSON_CreateString(" Mouse tail juice "));
cJSON_AddItemToArray(array,cJSON_CreateString(" Lightning whip "));
cJSON_AddItemToArray(array,cJSON_CreateString(" Don't talk about martial arts "));
cJSON_AddNumberToObject(json,"score",0);
cJSON_AddStringToObject(json,"address","xxx");
// take json Structure format to buffer
char *buf = cJSON_Print(json);
// Open file write json Content
FILE *fp = fopen("create.json","w");
fwrite(buf,strlen(buf),1,fp);
free(buf);
fclose(fp);
// Release json Memory occupied by structure
cJSON_Delete(json);
return 0;
}Output results ( Write to file ):
{
"name": " Ma Baoguo ",
" Age ": 69,
" Skill ": [" Mouse tail juice ", " Lightning whip ", " Don't talk about martial arts "],
"score": 0,
"address": "xxx"
}Then advance again :
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"cJSON.h"
int main(void)
{
// First create an empty object
cJSON *json = cJSON_CreateObject();
// Add a key value pair to the object
cJSON_AddStringToObject(json,"country","china");
// Add an array
cJSON *array = NULL;
cJSON_AddItemToObject(json,"stars",array=cJSON_CreateArray());
// Add object to array
cJSON *obj = NULL;
cJSON_AddItemToArray(array,obj=cJSON_CreateObject());
cJSON_AddItemToObject(obj,"name",cJSON_CreateString("Faye"));
cJSON_AddStringToObject(obj,"address","beijing");
// Add a key value pair to the object
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");
// Cleaning work
FILE *fp = fopen("create.json","w");
char *buf = cJSON_Print(json);
fwrite(buf,strlen(buf),1,fp);
fclose(fp);
cJSON_Delete(json);
return 0;
}Output results ( Write it in a file ):
{
"country": "china",
"stars": [{
"name": "Faye",
"address": "beijing"
}, {
"name": "andy",
"address": "HK"
}, {
"name": "eddie",
"address": "TaiWan"
}]
}compile :
gcc json_example.c cJSON.c -o json_example -L . -lmI believe after reading these examples , Write something a little more complicated json It's also completely OK. This thing is not difficult , Just be careful .
边栏推荐
- RestTemplate通过泛型实现POST、PUT、DELETE、GET、集合请求以及文件上传(可批量文件、可带参数)的统一封装(可打印日志)
- Redis source code and design analysis -- 18. Analysis of redis network connection Library
- LeetCode 101. 对称二叉树 && 100. 相同的树 && 572. 另一棵树的子树
- testng执行顺序的3中控制方法
- Postman get started quickly
- C语言 libcurl交叉编译
- Oracle导入出错:IMP-00038: 无法转换为环境字符集句柄
- CVE-2022-33891 Apache spark shell 命令注入漏洞复现
- SLA 、SLO & SLI
- go defer与recover简单笔记
猜你喜欢

Keil5 “Loading PDSC Debug Description Failed for STMicroelectronics STM32Hxxxxxxx”解决办法

What is the relationship between cloud fluidization and cloud desktop

TME2022校园招聘后台开发/运营开发/业务运维/应用开发笔试(I)编程题的一点自我分析

"Digital security" alert NFT's seven Scams

图的相关操作

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

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

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

关于云XR介绍,以及5G时代云化XR的发展机遇

Construction of Huffman tree
随机推荐
LeetCode 101. 对称二叉树 && 100. 相同的树 && 572. 另一棵树的子树
PageHelper can also be combined with lambda expressions to achieve concise paging encapsulation
Go defer and recover simple notes
Hit the test site directly: summary of common agile knowledge points in PMP examination
"Deprecated gradle features were used in this build, making it incompatible with gradle 6.0" problem solving
What is the relationship between cloud fluidization and cloud desktop
Idea essential plug-ins
mysql case when
SLA 、SLO & SLI
Which real-time gold trading platform is reliable and safe?
CH582 BLE 5.0 使用 LE Coded 广播和连接
Principle and implementation of UDP penetration NAT in P2P
MySQL数据库常用命令
Mock服务moco系列(一)- 简介、第一个Demo、Get请求、Post请求
2022/7/23
imx6 RTL8189FTV移植
PHP解决并发问题的几种实现
Take you to a preliminary understanding of multiparty secure computing (MPC)
Type assertion of go interface variables
Drawing PDF form (II) drawing excel form style in PDF through iText, setting Chinese font, watermark, logo, header and page number