Chinaunix首页 | 论坛 | 博客
  • 博客访问: 30103709
  • 博文数量: 230
  • 博客积分: 2868
  • 博客等级: 少校
  • 技术积分: 2223
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-08 21:48
个人简介

Live & Learn

文章分类

全部博文(230)

文章存档

2022年(2)

2019年(5)

2018年(15)

2017年(42)

2016年(24)

2015年(13)

2014年(1)

2012年(5)

2011年(58)

2010年(56)

2009年(9)

我的朋友

分类: 其他平台

2016-09-22 18:08:09

一、下载cJson文件
     实际只包含了cJSON.C,cJson.h,cJson_utils.c,cJson_utils.h
二、添加到工程
     
三、修改代码:
   在cJson_utils.c中增加

点击(此处)折叠或打开

  1. char * strdup(const char *s)
  2. {
  3.    size_t len = strlen(s) +1;
  4.    void *new = malloc(len);
  5.    if (new == NULL)
  6.       return NULL;
  7.    return (char *)strncpy(new,s,len);
  8. }


四、测试在main.c中增加


点击(此处)折叠或打开

  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include "cJSON.h"


  5. int main(void)
  6. {
  7. //SystemInit();
  8. char *out ;
  9. cJSON *root,*fmt;
  10. root=cJSON_CreateObject();//创建项目
  11. cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble"));
  12. cJSON_AddItemToObject(root, "format", fmt=cJSON_CreateObject());//在项目上添加项目
  13. cJSON_AddStringToObject(fmt,"type", "rect");//在项目上的项目上添加字符串,这说明cJSON是可以嵌套的
  14. cJSON_AddNumberToObject(fmt,"width", 1920);
  15. cJSON_AddNumberToObject(fmt,"height", 1080);
  16. cJSON_AddNumberToObject(fmt,"frame rate", 24);
  17. out=cJSON_Print(fmt);
  18. printf("%s\n",out);//此时out指向的字符串就是JSON格式的了
  19. free(out);//释放空间
  20. while(1);
  21. return 0;
  22. }




阅读(7671) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~