//在网上查了不少cJSON,结果只找到c语言字符串转换到JSON的实例,想转回来结果没有实例。自己琢磨了一个下午才敢下手。下面把转来转去的代码贴上。

//百度网盘的 CJSON 实例源码 地址 http://pan.baidu.com/s/1ntsRLgt

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

*先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,这样就可以不去考虑内存的释放了

jsoncpp http://www.cnblogs.com/fengbohello/p/4059435.html

或 http://www.cnblogs.com/fengbohello/p/4066254.html

作者:郝峰波

mail : fengbohello@qq.com

cJSON应用举例的更多相关文章

  1. 【转】cJSON 源码分析

    cJSON源码分析 简介 由于C语言汇总,没有直接的字典,字符串数组等数据结构,所以要借助结构体定义,处理json. JSON是一种轻量级的数据交换格式.JSON采用完全独立与语言的文本格式,易于人阅 ...

  2. Ajax 概念 分析 举例

    Ajax是结合了访问数据库,数据访问,Jquery 可以做页面局部刷新或者说是页面不刷新,我可以让页面不刷新,仅仅是数据的刷新,没有频繁的刷页面,是现在比较常用的一种方式做页面那么它是怎么实现页面无刷 ...

  3. ValueInjecter----最好用的OOM(以微信消息转对象举例)

    使用数据实体的好处我这里就不多说了,但大家享受这些好处的时候,难免也对那些琐碎的赋值代码感到厌烦,基于此,我认为掌握一个oom的使用,还是很有必要的. 这种类型的工具有很多,比如automapper, ...

  4. Oracle简单常用的数据泵导出导入(expdp/impdp)命令举例(上)

    <Oracle简单常用的数据泵导出导入(expdp/impdp)命令举例(上)> <Oracle简单常用的数据泵导出导入(expdp/impdp)命令举例(下)> 目的:指导项 ...

  5. Oracle简单常用的数据泵导出导入(expdp/impdp)命令举例(下)

    <Oracle简单常用的数据泵导出导入(expdp/impdp)命令举例(上)> <Oracle简单常用的数据泵导出导入(expdp/impdp)命令举例(下)> 目的:指导项 ...

  6. shell-script的简单举例

    #!/bin/bash #defind the path PATH=/usr/local export PATH read -p "please input your first name: ...

  7. 各大浏览器内核特性及对应的Browserhacks举例

    1.浏览器内核指的是什么? 简化的浏览器=用户界面+渲染引擎+js解析引擎+数据存储+网络部件 而通常所说的浏览器内核指的是页面渲染引擎(rendering engine). 2.渲染引擎 The r ...

  8. js调用php和php调用js的方法举例

    js调用php和php调用js的方法举例1 JS方式调用PHP文件并取得php中的值 举一个简单的例子来说明: 如在页面a.html中用下面这句调用: <script type="te ...

  9. c++局部变量经典举例

    局部变量: 在函数内部声明的变量为局部变量,局部变量的意思即该变量只存活在该函数中,假如该函数调用结束,那么该变量的寿命也结束了. 举例: #include<iostream> using ...

随机推荐

  1. 一段功能齐全的PHP常用重定向代码html+js+header

    /** * 重定向浏览器到指定的 URL * * @param string $url 要重定向的 url * @param int $delay 等待多少秒以后跳转 * @param bool $j ...

  2. SQL union和union all的区别

    Union因为要进行重复值扫描,所以效率低.如果合并没有刻意要删除重复行,那么就使用Union All  两个要联合的SQL语句 字段个数必须一样,而且字段类型要“相容”(一致): 如果我们需要将两个 ...

  3. 使用clssneme改变图片或样式

    <title>使用className改变样式</title> <style type="text/css"> li{ font-size: 12 ...

  4. Java for LeetCode 151 Reverse Words in a String

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  5. Java for LeetCode 139 Word Break

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  6. percona-xtrabackup备份mysql

    title: 1.percona-xtrabackup备份mysql date: 2016-04-10 23:19:12 tags: mysql categories: mysql --- 一.per ...

  7. codeforces A. Table 解题报告

    题目链接:http://codeforces.com/problemset/problem/359/A 题目意思:给出一个n行m列的table,你需要选择一个good cell(假设为(x, y), ...

  8. jquery去掉或者替换字符,设置指定options为selected状态

    <html> <body> <div><select id="queryYear">                 <opt ...

  9. 部署django应用

    Autor: wangxianlong 2016/7/10 16:17:55 环境: centos 6.5 python 2.7.5 django 1.9 nginx 1.8 selinux diab ...

  10. GLSL Entry point not found

    解决方案: 在引用OpenGL的头文件 #include <GL/glew.h>#include <GL/glut.h> 前添加 #define GLUT_DISABLE_AT ...