Use of C language cJSON library

First download the cJSON library first, it is still easy to find the way to download

Unzip the compressed package, put the cJSON.h and cJSON.c inside into your project, and you can start playing

First, a simple code:

#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;
}

Output result:

data:{
	"jjj":	["kkk", "lll"]
}

Then you can advance a bit, the code is as follows:

#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;
}

Output result (written to file):

{
	"name":	"马保国",
	"年龄":	69,
	"技能":	["耗子尾汁", "闪电鞭", "不讲武得"],
	"score":	0,
	"address":	"xxx"
}

Then go one more step:

#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;
}

Output result (write to 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 . -lm

I believe that after reading these few examples, it is not difficult to write that kind of more complicated json, which is completely fine. Just be careful.

Guess you like

Origin blog.csdn.net/smile_5me/article/details/110930727
Recommended