当前位置:网站首页>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 .
边栏推荐
- Basic operation of bidirectional linked list
- go接口变量的类型断言
- TME2022校园招聘后台开发/运营开发/业务运维/应用开发笔试(I)编程题的一点自我分析
- 2022/7/23
- OV7725 yuv 640*[email protected] 配置文件
- What is the relationship between cloud fluidization and cloud desktop
- 推荐一个沁恒的蓝牙的参考博客
- Hcip first day experiment
- Basic knowledge of software testing (mind mapping)
- What is an IP SSL certificate and how to apply for it?
猜你喜欢

十九岁的总结
![Why is the index in [mysql] database implemented by b+ tree? Is hash table / red black tree /b tree feasible?](/img/1f/a2d50ec6bc97d52c1e7566a42e564b.png)
Why is the index in [mysql] database implemented by b+ tree? Is hash table / red black tree /b tree feasible?

Idea integrates common functions of SVN code management

Auditing相关注解

Highlights

The new version of 3dcat v2.1.3 has been released. You can't miss these three function updates!

Memory and packet buffer management of LwIP

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

Take you to a preliminary understanding of multiparty secure computing (MPC)

「行话」| 用DevOps高效交付游戏,是种什么体验?
随机推荐
"Deprecated gradle features were used in this build, making it incompatible with gradle 6.0" problem solving
关于云XR介绍,以及5G时代云化XR的发展机遇
有没有什么不起眼却挣钱的副业?
Ch582 ble 5.0 uses Le coded broadcast and connection
「行话」| 用DevOps高效交付游戏,是种什么体验?
Is there any inconspicuous but profitable sideline?
UFT(QTP)-总结点与自动化测试框架
Hcip first day experiment
go defer与recover简单笔记
Li Kai: the interesting and cutting-edge audio and video industry has always attracted me
推荐一个沁恒的蓝牙的参考博客
Mock service Moco series (II) - JSON format, file file, header, cookie, solving Chinese garbled code
Recommend a qinheng Bluetooth reference blog
软件测试基础知识(思维导图)
UnitTest框架应用
Redis source code and design analysis -- 17. Redis event processing
What scenarios have rust, which is becoming more and more mature, applied?
Mongodb cluster and sharding
What is an IP SSL certificate and how to apply for it?
Go channel simple notes