參考文章:
http://json-c.github.io/json-c/json-c-0.13.1/doc/html/index.html
1 簡介
簡單介紹下json-c庫的一些函數。
2 下載及編譯
2.1下載地址
http://www.linuxfromscratch.org/blfs/view/svn/general/json-c.html
2.2 編譯
在編譯前需要Linux PC上確認以下被安裝:
apt-get install autoconf automake libtool
2.2.1 交叉編譯
./configure CC=arm-linux-gcc --host=arm-linux --prefix=$PWD/install
make && make install
2.2.2 PC Linux編譯
./configure --prefix=$PWD/install
make && make install
3 函數介紹
3.1 json_object_new_object
函數原型:
JSON_EXPORT struct json_object* json_object_new_object(void);
函數參數:
無
函數返回:
struct json_object*指針
說明:
創建一個新的json_object對象。
3.2 json_object_object_add
函數原型:
JSON_EXPORT int json_object_object_add(struct json_object* obj, const char *key,
struct json_object *val);
函數參數:
obj:要被添加的json_object對象
key:字段名稱
val:與key關聯的json_object對象
函數返回:
0:成功
<0:失敗
說明:
將字段名稱爲key的json_object對象val添加進json_object對象obj中去
3.3 json_object_new_int
函數原型:
JSON_EXPORT struct json_object* json_object_new_int(int32_t i);
函數參數:
i:數值
函數返回:
新的json_object對象
說明:
創建一個類型爲int的json_object新對象
3.4 json_object_new_array
函數原型:
JSON_EXPORT struct json_object* json_object_new_array(void);
函數參數:
無
函數返回:
新的json_object對象
說明:
創建一個類型爲array的json_object新對象
3.5 json_object_array_add
函數原型:
JSON_EXPORT int json_object_array_add(struct json_object *obj,
struct json_object *val);
函數參數:
obj:要被添加的json_object對象
val:與添加到obj的json_object對象
函數返回:
0:成功
<0:失敗
說明:
將json_object對象val添加到obj中
3.6 json_object_new_string
函數原型:
JSON_EXPORT struct json_object* json_object_new_string(const char *s);
函數參數:
s:字符串
函數返回:
新的json_object對象
說明:
創建一個類型爲string的json_object新對象
3.7 json_tokener_parse
函數原型:
JSON_EXPORT struct json_object* json_tokener_parse(const char *str);
函數參數:
str:包含json數據的字符串
函數返回:
json_object對象
說明:
從str中解析出json_object對象
3.8 json_object_object_get_ex
函數原型:
extern json_bool json_object_object_get_ex(struct json_object* obj,
const char *key,
struct json_object **value);
函數參數:
obj:json_object對象
key:要獲取的字段名稱
val:與key關聯的json_object對象
函數返回:
非0:成功
0:失敗
說明:
從obj中獲取字段爲key的json_object對象指針val
3.9 json_object_get_type
函數原型:
extern enum json_type json_object_get_type(struct json_object *obj);
函數參數:
obj:json_object對象
函數返回:
json_object的類型
說明:
類型如下:
typedef enum json_type {
json_type_null,
json_type_boolean,
json_type_double,
json_type_int,
json_type_object,
json_type_array,
json_type_string,
} json_type;
3.10 json_object_to_json_string
函數原型:
JSON_EXPORT const char* json_object_to_json_string(struct json_object *obj);
函數參數:
obj:json_object對象
函數返回:
字符串指針
說明:
將生成的json_object輸出爲字符串格式
3.11 json_object_put
函數原型:
JSON_EXPORT int json_object_put(struct json_object *obj);
函數參數:
obj:要被釋放的對象
函數返回:
1:成功
說明:
釋放掉json_object內存空間