linux 下如何使用cJSON庫

cJSON簡介:

JSON(JavaScriptObject Notation)是一種輕量級的數據交換格式。它基於JavaScript的一個子集。JSON採用完全獨立於語言的文本格式,但是也使用了類似於C語言家族的習慣。這些特性使JSON成爲理想的數據交換語言。易於人閱讀和編寫,同時也易於機器解析和生成。

cJSON是一個超輕巧,攜帶方便,單文件,簡單的可以作爲ANSI-C標準的JSON解析器。瞭解json請參考其官網http://json.org/,cJSON 下載地址http://sourceforge.net/projects/cjson/?source=typ_redirect,文件只有幾十k,若無法進入下載界面,請到我分享的資源裏下載,下載完成後解壓,內容如下:

test.c 是下載後自帶的一個小小的例子,通過這個例子的學習,很快就能掌握json數據結構的構造和解析。

自帶例子編譯方式如下:

gcc cJSON.c test.c -o jsontest  -lm  

此處記住一定要包含 -lm,因爲cJSON.c裏面用到了兩個數學函數pow floor,否則編譯會報錯。

參考文獻出處:

http://blog.csdn.net/wangpengqi/article/details/9412211

http://blog.csdn.net/daisy09/article/details/7086746

一、json庫的簡單介紹和簡單實際應用

cJSON結構體:
typedefstruct cJSON {
structcJSON *next,*prev;
struct cJSON *child;
int type;
char * valuestring;
int valueint;
double valuedouble;
char *string;
}cJSON;


1、cJSON存儲的時候是採用鏈表存儲的,其訪問方式很像一顆樹。每一個節點可以有兄妹節點,通過next/prev指針來查找,它類似雙向鏈表;每個節點也可以有孩子節點,通過child指針來訪問,進入下一層。不過,只有節點是對象或數組纔可以有孩子節點。
2、type一共有7種取值,分別是:
#define cJSON_False 0
#define cJSON_True 1
#define cJSON_NULL 2
#define cJSON_Number 3
#define cJSON_String 4
#define cJSON_Array 5
#define cJSON_Object 6
若是Number類型,則valueint或valuedouble中存儲着值,若你期望的是int,則訪問valueint,若期望的是double,則訪問valuedouble,可以得到值。
若是String類型的,則valuestring中存儲着值,可以訪問valuestring得到值。
3、string中存放的是這個節點的名字
二、用法舉例
自己根據自帶的測試程序編寫了一個構造和解析json的例子,代碼不是很漂亮,主要是爲了介紹一下如何使用json庫

/*
  Copyright (c) 2009 Dave Gamble
 
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
 
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
 
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/

#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"

/* Parse text to JSON, then render back to text, and print! */
void doit(char *text)
{
	char *out;cJSON *json;
	
	json=cJSON_Parse(text);
	if (!json) {printf("Error before: [%s]\n",cJSON_GetErrorPtr());}
	else
	{
		out=cJSON_Print(json);
		cJSON_Delete(json);
		printf("%s\n",out);
		free(out);
	}
}
void create_objects()
{
	cJSON *root,*fmt,*img,*thm,*fld;char *out, *out1;int i;	/* declare a few. */

	/* Here we construct some JSON standards, from the JSON site. */
	
	/* Our "Video" datatype: */
	root=cJSON_CreateObject();	
	cJSON_AddItemToObject(root, "name", cJSON_CreateString("Yuwc"));
	cJSON_AddItemToObject(root, "format", fmt=cJSON_CreateObject());
	cJSON_AddStringToObject(fmt,"type",		"rect");
	cJSON_AddNumberToObject(fmt,"width",		1920);
	cJSON_AddNumberToObject(fmt,"height",		1080);
	cJSON_AddFalseToObject (fmt,"interlace");
	cJSON_AddNumberToObject(fmt,"frame rate",	24);
	cJSON *format = cJSON_GetObjectItem(root,"format");   
	char *name = cJSON_GetObjectItem(root,"name")->valuestring;
	printf("name=[%s]\n", name); 

	int framerate = cJSON_GetObjectItem(format,"height")->valueint; 
	printf("%d\n", framerate); 
	char *type = cJSON_GetObjectItem(format,"type")->valuestring;
	printf("%s\n", type); 
	out=cJSON_Print(root);	cJSON_Delete(root);	printf("%s\n",out);	free(out);
	
int main (int argc, const char * argv[])
{
	char text1[]="{\n\"name\": \"Jack (\\\"Bee\\\") Nimble\", \n\"format\": {\"type\":       \"rect\", \n\"width\":      1920, \n\"height\":     1080, \n\"interlace\":  false,\"frame rate\": 24\n}\n}";	
	//string輸出Json格式
	doit(text1);
	create_objects();
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章