cJSON應用舉例


//在網上查了不少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

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM