c語言解析json

c語言解析json
一,json簡介
JSON(JavaScript Object Notation) 是一種輕量級的數據交換格式。 易於人閱讀和編寫。同時也易於機器解析和生成。 它基於JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一個子集。 JSON採用完全獨立於語言的文本格式,但是也使用了類似於C語言家族的習慣(包括C, C++, C#, Java, JavaScript, Perl, Python等)。 這些特性使JSON成爲理想的數據交換語言。
JSON建構於兩種結構:
“名稱/值”對的集合(A collection of name/value pairs)。不同的語言中,它被理解爲對象(object),紀錄(record),結構(struct),字典(dictionary),哈希表(hash table),有鍵列表(keyed list),或者關聯數組 (associative array)。
值的有序列表(An ordered list of values)。在大部分語言中,它被理解爲數組(array)。
這些都是常見的數據結構。事實上大部分現代計算機語言都以某種形式支持它們。這使得一種數據格式在同樣基於這些結構的編程語言之間交換成爲可能。
JSON具有以下這些形式:
對象是一個無序的“‘名稱/值’對”集合。一個對象以“{”(左括號)開始,“}”(右括號)結束。每個“名稱”後跟一個“:”(冒號);“‘名稱/值’ 對”之間使用“,”(逗號)分隔。

數組是值(value)的有序集合。一個數組以“[”(左中括號)開始,“]”(右中括號)結束。值之間使用“,”(逗號)分隔。

值(value)可以是雙引號括起來的字符串(string)、數值(number)、true、false、 null、對象(object)或者數組(array)。這些結構可以嵌套。

字符串(string)是由雙引號包圍的任意數量Unicode字符的集合,使用反斜線轉義。一個字符(character)即一個單獨的字符串(character string)。
字符串(string)與C或者Java的字符串非常相似。

數值(number)也與C或者Java的數值非常相似。除去未曾使用的八進制與十六進制格式。除去一些編碼細節。

空白可以加入到任何符號之間。 

二,json-c庫說明
3.1 JSON對象的生成
3.1.1,Json對象的類型:
json_type_object, “名稱/值”對的集合
Json對象的值類型
json_type_boolean,
json_type_double, 
json_type_int,
json_type_array, “值”的集合
json_type_string

3.1.2,struct json_object * json_object_new_object();
說明:創建個空的json_type_object類型JSON對象。
struct json_object* json_object_new_boolean(boolean b);
說明:創建個json_type_boolean值類型json對象
boolean json_object_get_boolean(struct json_object *obj);
說明:從json對象中boolean值類型得到boolean值
同樣:
struct json_object* json_object_new_int(int i)
int json_object_get_int(struct json_object *this)
struct json_object* json_object_new_double(double d)
double json_object_get_double(struct json_object *this)
struct json_object* json_object_new_string(char *s)
char* json_object_get_string(struct json_object *this)

3.1.3,struct json_object * json_object_new_array();
說明:創建個空的json_type_array類型JSON數組值對象。
struct json_object * json_tokener_parse(char *str);
說明:由str裏的JSON字符串生成JSON對象,str是son_object_to_json_string() 生成的。
參數:str – json字符串

3.1.4,struct json_object * json_object_object_get(struct json_object * json,char *name);
說明:從json中按名字取一個對象。
參數:
json – json對象
name -  json域名字

3.2,JSON對象的釋放
struct json_object * * json_object_get(struct json_object * this)
說明: 增加對象引用計數。使用c庫最關心的是內存誰來分配, 誰來釋放. jsonc的內存管理方式, 是基於引用計數的內存樹(鏈), 如果把一個struct json_object 對象a, add到另一個對象b上, 就不用顯式的釋放(json_object_put) a了, 相當於把a掛到了b的對象樹上, 釋放b的時候, 就會釋放a. 當a即add到b上, 又add到對象c上時會導致a被釋放兩次(double free), 這時可以增加a的引用計數(調用函數json_object_get(a)), 這時如果先釋放b, 後釋放c, 當釋放b時, 並不會真正的釋放a, 而是減少a的引用計數爲1, 然後釋放c時, 才真正釋放a.
參數:
this – json對象


void json_object_put(struct json_object * this)
說明:
減少對象引用次數一次,當減少到0就釋放(free)資源
參數:
this – json對象

3.3 JSON對象的操作
int json_object_is_type(struct json_object * this, enum json_type type)
說明:
檢查json_object是json的某個類型
參數:
this: json_object 實例
type: json_type_boolean,json_type_double, json_type_int, json_type_object, json_type_array, json_type_string

enum json_type json_object_get_type(struct json_object * this ) 
說明:
得到json_object的類型。
參數:
this – json對象

char * json_object_to_json_string(struct json_object * this)
說明:
將json_object內容轉換json格式字符串,其中可能含有轉義符。
參數:
this – json對象
返回值:
Json格式字符串

void json_object_object_add(struct json_object* obj, char *key, struct json_object *val);
說明:
添加個對象域到json對象中
參數:
Obj – json對象
key – 域名字
val – json值對象

void json_object_object_del(struct json_object* obj, char *key);
說明:
刪除key值json對象
參數:
ob j – json對象
key – 域名字

int json_object_array_length(struct json_object *obj);
說明:
得到json對象數組的長度。
參數:
ob j – json數組值對象

extern int json_object_array_add(struct json_object *obj,
struct json_object *val);
說明:
添加一元素在json對象數組末端
參數:
ob j – json數組值對象
val – json值對象

int json_object_array_put_idx(struct json_object *obj, int idx,
    struct json_object *val);
說明:
在指定的json對象數組下標插入或替換一個json對象元素。
參數:
ob j – json數組值對象
val – json值對象
  idx – 數組下標

struct json_object * json_object_array_get_idx(struct json_object * json_array,int i);
說明:
從數組中,按下標取JSON值對象。
參數:
json_array – json 數組類型對象
i – 數組下標位置

定義宏 json_object_object_foreach(obj,key,val)
說明:
遍歷json對象的key和值 (key, val默認參數不變)

extern struct json_object* json_object_from_file(const char *filename);
extern int json_object_to_file(const char *filename, struct json_object *obj);
類似序列化的函數

參考:http://www.json.org/json-zh.html
發佈了124 篇原創文章 · 獲贊 15 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章