基本信息
源码名称:Json 的C语言实现
源码大小:9.57KB
文件格式:.zip
开发语言:C/C++
更新时间:2017-07-18
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 4 元 
   源码介绍

    这是一个C语言实现的Json分析器,C语言实现Json操作


#include <stdio.h>
#include "CJSON.h"

int main()
{
	CJSON * pJsonRoot = NULL;

	pJsonRoot = CJSON_CreateObject();
	if(NULL == pJsonRoot)
	{
		//error happend here
		return 0;
	}
	CJSON_AddStringToObject(pJsonRoot, "hello", "hello world");
	CJSON_AddNumberToObject(pJsonRoot, "number", 10010);
	CJSON_AddBoolToObject(pJsonRoot, "bool", 1);
	CJSON * pSubJson = NULL;
	pSubJson = CJSON_CreateObject();
	if(NULL == pSubJson)
	{
		// create object faild, exit
		CJSON_Delete(pJsonRoot);
		return 0;
	}
	CJSON_AddStringToObject(pSubJson, "subjsonobj", "a sub json string");
	CJSON_AddItemToObject(pJsonRoot, "subobj", pSubJson);

	char * p = CJSON_Print(pJsonRoot);
	if(NULL == p)
	{
		//convert json list to string faild, exit
		//because sub json pSubJson han been add to pJsonRoot, so just delete pJsonRoot, if you also delete pSubJson, it will coredump, and error is : double free
		CJSON_Delete(pJsonRoot);
		return 0;
	}
	printf("%s\n", p);

	return 0;
}