本文为转载

原文地址:http://blog.sina.com.cn/s/blog_4c0cb1c00102xg7j.html

使用说明:将cJSON.c、cJSON.h两个文件,拷贝到工程项目文件中编译使用即可。

下载地址:https://sourceforge.net/projects/cjson/

主要函数:

【1】常用创建
【创建JSON对象】cJSON *cJSON_CreateObject(void);
【创建JSON数组】cJSON *cJSON_CreateArray(void); 【2】常用添加
【向对象中添加对象】voidcJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item);
【向数组中添加对象】void cJSON_AddItemToArray(cJSON *array, cJSON *item);
【向对象中添加数值】cJSON_AddNumberToObject(object,name,n)
【向对象中添加字符串】 cJSON_AddStringToObject(object,name,s)
示例:
#include
#include "cJSON.h" char *makeJson()
{
cJSON *pJsonRoot = NULL;
cJSON *pIntArray = NULL;
cJSON *pCommArray = NULL;
cJSON *pSubJson = NULL;
char *p = NULL;
int intarr[5] = {0, 1, 2, 3, 4}; //整数数组 pJsonRoot = cJSON_CreateObject(); //创建JSON对象 if(NULL == pJsonRoot)
{
//error happend here
return NULL;
} cJSON_AddStringToObject(pJsonRoot, "hello", "hello world"); //添加一个值为字符串的键值对"hello":"hello world"
cJSON_AddNumberToObject(pJsonRoot, "number", 10010); //添加一个值为数值的键值对"number": 10010
cJSON_AddBoolToObject(pJsonRoot, "bool", 1); //添加一个值为布尔的键值对"bool": 1 pSubJson = cJSON_CreateObject(); //创建另一个json对象作为子对象 if(NULL == pSubJson)
{
// create object faild, exit
cJSON_Delete(pJsonRoot); //删除pJsonRoot 及其子对象
return NULL;
} cJSON_AddStringToObject(pSubJson, "subjsonobj", "a sub json string"); //添加一个值为布尔的键值对
cJSON_AddItemToObject(pJsonRoot, "subobj", pSubJson); //将对象pSubJson添加到pJsonRoot中,成为键值对 "subobj":pSubJson //数值数组
pIntArray = cJSON_CreateIntArray(intarr, 5); //为intarr创建一个数值数组对象,
cJSON_AddItemToObject(pJsonRoot, "IntArr", pIntArray); //将对象pIntArray添加到pJsonRoot中,成为键值对 " IntArr ":pIntArray //通用数组 pCommArray = cJSON_CreateArray();//创建数组对象
//cJSON_AddItemToArray(cJSON *array, cJSON *item);//向数组中添加对象
cJSON_AddNumberToObject(pCommArray, "num", 2); //向数组中添加数值cJSON_AddNumberToObject(object,name,n)
cJSON_AddStringToObject(pCommArray, "str", "hopeview"); //向对象中添加字符串 cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(pJsonRoot, "pCommArray ", pCommArray); //将对象pCommArray添加到pJsonRoot中,成为键值对 " pCommArray ":pCommArray p = cJSON_Print(pJsonRoot); //json对象转为json字符串用于传输 // else use :
// char * p = cJSON_PrintUnformatted(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); //删除pJsonRoot 及其子对象
return NULL;
} //free(p); cJSON_Delete(pJsonRoot); //释放json对象
printf("myJson is:%s",p);
return p; //返回json字符串,注意外面用完p要记得释放空间。//free(p);
}

最新文章

  1. dotnet core 出现Can not find runtime target for framework '.NETCoreApp,Version=v1.6' 的解决办法
  2. 多线程_先产后销_运行结果有BUG
  3. 基于Selenium的自动化测试 C#版(1)
  4. POJ3087Shuffle'm Up(map)
  5. [HDOJ4022]Bombing(离散化+stl)
  6. ListView 复制到剪切板
  7. 使用Notify 和 wait ,使用Linklist实现生产者消费者问题
  8. React入门教程
  9. Golang http 服务器
  10. 在Vue.js2.0中组件模板子元素数量问题
  11. 通过java代码执行Linux命令查询声卡和显卡 型号
  12. 简单的SSM框架搭建教程
  13. 【js】实现继承的6种方法
  14. Table does not have the identity property. Cannot perform SET operation.
  15. ios中非ARC项目中引用ARC文件
  16. 小程序文件上传uploadFile
  17. vmp3.0.9全保护拆分解析
  18. HOJ 2985 Wavio Sequence(最长递增子序列以及其O(n*logn)算法)
  19. c# &与&& 和 |与||的区别
  20. js往div里添加table

热门文章

  1. mysql版本
  2. swan.after
  3. php实现进度条原理
  4. Best Practices For Running On The PS4
  5. IDEA搭建spingboot项目
  6. 神坑 Resources.Load 不能实时加载TXT文件
  7. 使用IDEA在引入Schema空间时报错URI is not registered解决方法以及Idea @Autowired取消提示 方法
  8. (转)原理到实现 | K8S 存储之 NFS
  9. How-To-Ask-Questions-The-Smart-Way提问的技巧 提问的智慧
  10. 135、TensorFlow SavedModel工具类的使用