---
title: "cJsonFiles数据结构"
author: "vergilwang"
date: "2013-04-27T17:21:00Z"
tags: []
categories: ["编程语言"]
slug: "cjsonfiles"
draft: false
lastmod: "2014-02-15T14:56:15Z"
id: 2011219
url: "https://vergilwang.iteye.com/blog/2011219"
---

先看json的数据结构

c中没有对象，所以json数据是采用链表存储的

[view source](http://www.bearmao.com/post_218.html#viewSource "view source")[print](http://www.bearmao.com/post_218.html#printSource "print")[?](http://www.bearmao.com/post_218.html#about "?")

`01.typedef struct cJSON {`

`02.struct cJSON *next,*prev;<wbr><wbr><wbr>// 数组 对象数据中用到</wbr></wbr></wbr>`

`03.struct cJSON *child;<wbr><wbr><wbr><wbr><wbr><wbr>// 数组 和对象中指向子数组对象或值</wbr></wbr></wbr></wbr></wbr></wbr>`

`04.<wbr></wbr>`&lt;wbr&gt;&lt;/wbr&gt;

`05.int type;<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>// 元素的类型，如是对象还是数组</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>`

`06.<wbr></wbr>`&lt;wbr&gt;&lt;/wbr&gt;

`07.char *valuestring;<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>// 如果是字符串</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>`

`08.int valueint;<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>// 如果是数值</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>`

`09.doublevaluedouble;<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>// 如果类型是cJSON_Number</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>`

`10.<wbr></wbr>`&lt;wbr&gt;&lt;/wbr&gt;

`11.char *string;<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>// The item's name string, if this item is the child of, or is in the list of subitems of an object.</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>`

`12.} cJSON;`

比如你有一个json数据

[view source](http://www.bearmao.com/post_218.html#viewSource "view source")[print](http://www.bearmao.com/post_218.html#printSource "print")[?](http://www.bearmao.com/post_218.html#about "?")

`01.{`

`02."name":"Jack (\"Bee\") Nimble",`

`03."format": {`

`04."type":<wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>"rect",`

`05."width":<wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr>1920,`

`06."height":<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>1080,`

`07."interlace":<wbr>false,</wbr>`

`08."frame rate":24`

`09.}`

`10.}`

那么你可以

1：讲字符串解析成json结构体。

cJSON&lt;wbr&gt;\*root&lt;wbr&gt;=&lt;wbr&gt;cJSON\_Parse(my\_json\_string);&lt;br&gt; 2:获取某个元素&lt;/wbr&gt;&lt;/wbr&gt;&lt;/wbr&gt;

cJSON&lt;wbr&gt;\*format&lt;wbr&gt;=&lt;wbr&gt;cJSON\_GetObjectItem(root,”format”);&lt;/wbr&gt;&lt;/wbr&gt;&lt;/wbr&gt;

int&lt;wbr&gt;framerate&lt;wbr&gt;=&lt;wbr&gt;cJSON\_GetObjectItem(format,”frame&lt;wbr&gt;rate”)-&gt;valueint;&lt;/wbr&gt;&lt;/wbr&gt;&lt;/wbr&gt;&lt;/wbr&gt;

3:讲json结构体转换成字符串

char&lt;wbr&gt;\*rendered=cJSON\_Print(root);&lt;/wbr&gt;

4：删除

cJSON\_Delete(root);

5：构建一个json结构体

[view source](http://www.bearmao.com/post_218.html#viewSource "view source")[print](http://www.bearmao.com/post_218.html#printSource "print")[?](http://www.bearmao.com/post_218.html#about "?")

`01.cJSON *root,*fmt;`

`02.root=cJSON_CreateObject();`

`03.cJSON_AddItemToObject(root,"name", cJSON_CreateString("Jack (\"Bee\") Nimble"));`

`04.cJSON_AddItemToObject(root,"format", fmt=cJSON_CreateObject());`

`05.cJSON_AddStringToObject(fmt,"type",<wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>"rect");`

`06.cJSON_AddNumberToObject(fmt,"width",<wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>1920);`

`07.cJSON_AddNumberToObject(fmt,"height",<wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>1080);`

`08.cJSON_AddFalseToObject (fmt,"interlace");`

`09.cJSON_AddNumberToObject(fmt,"frame rate",<wbr><wbr><wbr></wbr></wbr></wbr>24);`