cJSON应用举例

简介:

*先C转成JSON的字符串,然后再把这个JSON的字符串转回来。

 

*******************************************/

 

#include "stdio.h"

 

#include "cjson.h"

 

/*******************************

 

* 建一个工程把"cjson.c"也加进去。

 

* 要是不想建工程,那就把下面这个注释去掉。

 

* 虽然正常人不这么干,但图个方便,也不管那么多了。

 

*******************************/

 

//#include "cjson.c"

 

int main_()
{

 

  //首先是用C转换成JSON
char *out ;
cJSON *root,*fmt;
root=cJSON_CreateObject();//创建项目
cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble"));
cJSON_AddItemToObject(root, "format", fmt=cJSON_CreateObject());//在项目上添加项目
cJSON_AddStringToObject(fmt,"type", "rect");//在项目上的项目上添加字符串,这说明cJSON是可以嵌套的
cJSON_AddNumberToObject(fmt,"width", 1920);
cJSON_AddNumberToObject(fmt,"height", 1080);
cJSON_AddNumberToObject(fmt,"frame rate", 24);

 

out=cJSON_Print(fmt);
printf("%s\n",out);//此时out指向的字符串就是JSON格式的了
free(out);//释放空间

 

  //接下来进行JSON格式向回转换

 

  cJSON *fmt = NULL,*JSONroot = NULL;

 

  num = cJSON_GetArraySize(JSONroot);//看看有多少个项目

 

  fmt = cJSON_GetObjectItem(JSONroot,"name");

 

  char name[256];

 

  snprintf(name,256,"%s",fmt->valuestring);//把fmt指向的JSON节点的字符串复制到name数组里来。

 

  //JSON是采用链式存储的,就是链表存储。具体的结构体可以在"cjson.h"里面找到

 

  /* The cJSON structure: */
//typedef struct cJSON {
// struct cJSON *next,*prev; /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
// struct cJSON *child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */

// int type; /* The type of the item, as above. */

// char *valuestring; /* The item's string, if type==cJSON_String */
// int valueint; /* The item's number, if type==cJSON_Number */
// double valuedouble; /* The item's number, if type==cJSON_Number */

// char *string; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
//} cJSON;

 

  cJSON *child;

 

  fmt = cJSON_GetObjectItem(JSONroot,"format");

 

  child = cJSON_GetObjectItem(fmt,"type");

 

  char type[256];

 

  snprintf(type,256,"%s",child->valuestring);

 

  child = cJSON_GetObjectItem(fmt,"width");

 

  int width = child->valueint;

 

  child = cJSON_GetObjectItem(fmt,"height");

 

  int heigh = child->valueint;

 

  child = cJSON_GetObjectItem(fmt,"frame rate");

 

  int frame = child->valueint;

 

return 0;
}

 

补充一篇CJSON实例(创建和解析json对象、创建和解析json数组)《使用 CJSON 在C语言中进行 JSON 的创建和解析的实例讲解

 不过使用cJSON的时候要注意,不要忘了cJSON_Delete()释放JSON的内存,和用free()释放cJSON_Print()或者cJSON_PrintUnformatted()返回的内存,否则会造成内存混乱。

如果不想纠结内存管理方面的问题,可以考虑使用jsoncpp,这样就可以不去考虑内存的释放了



本文转自郝峰波博客园博客,原文链接:http://www.cnblogs.com/fengbohello/p/3226624.html,如需转载请自行联系原作者

相关文章
|
6月前
|
JSON C语言 数据格式
使用cJSON库实现JSON与C结构体的互转
在实际应用中,我们经常需要将JSON格式的数据与C语言中的结构体进行相互转换。cJSON是一个非常便捷的C语言JSON解析库,它可以帮助我们在C语言中轻松地处理JSON数据。本文将介绍如何使用cJSON库来实现JSON数据与C结构体的互转。
262 2
ThinkPHP5使用include多次引入文件传入变量问题
ThinkPHP5使用include多次引入文件传入变量问题
213 0
|
XML 存储 JSON
C/C++程序开发: cJSON的使用(创建与解析JSON数据)
C/C++程序开发: cJSON的使用(创建与解析JSON数据)
719 0
C/C++程序开发: cJSON的使用(创建与解析JSON数据)
|
移动开发 小程序 PHP
PHP 字符串中直接解析函数的写法
PHP 中的字符串理论上是不能够解析函数的,仅能够解析变量。最近发现一种特殊的写法,是可以让字符串直接解析函数的。
112 0
PHP 字符串中直接解析函数的写法
|
PHP
thinkphp模板输出变量使用一个或多个函数
我们往往需要对模板输出变量使用函数,可以使用: {$data.name|md5}  编译后的结果是: 如果函数有多个参数需要调用,则使用: {$create_time|date="y-m-d",###} 表示date函数传入两个参数,每个参数用逗号分割,这里第一...
1237 0

热门文章

最新文章