json c使用 介紹

2021-10-01 11:18:19 字數 3404 閱讀 6486

3 函式介紹

參考文章:

簡單介紹下json-c庫的一些函式。

在編譯前需要linux pc上確認以下被安裝:

apt-get install autoconf automake libtool
./configure cc=arm-linux-gcc --host=arm-linux --prefix=$pwd/install

make && make install

./configure --prefix=$pwd/install

make && make install

函式原型:

json_export struct json_object* json_object_new_object(void);
函式引數:

函式返回:

struct json_object*指標
說明:

建立乙個新的json_object物件。

函式原型:

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中去

函式原型:

json_export struct json_object* json_object_new_int(int32_t i);
函式引數:

i:數值
函式返回:

新的json_object物件
說明:

建立乙個型別為int的json_object新物件

函式原型:

json_export struct json_object* json_object_new_array(void);
函式引數:

函式返回:

新的json_object物件
說明:

建立乙個型別為array的json_object新物件

函式原型:

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中

函式原型:

json_export struct json_object* json_object_new_string(const char *s);
函式引數:

s:字串
函式返回:

新的json_object物件
說明:

建立乙個型別為string的json_object新物件

函式原型:

json_export struct json_object* json_tokener_parse(const char *str);
函式引數:

str:包含json資料的字串
函式返回:

json_object物件
說明:

從str中解析出json_object物件

函式原型:

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

函式原型:

extern enum json_type json_object_get_type(struct json_object *obj);
函式引數:

obj:json_object物件
函式返回:

json_object的型別
說明:

型別如下:

typedef enum json_type  json_type;
函式原型:

json_export const char* json_object_to_json_string(struct json_object *obj);
函式引數:

obj:json_object物件
函式返回:

字串指標
說明:

將生成的json_object輸出為字串格式

函式原型:

json_export int json_object_put(struct json_object *obj);
函式引數:

obj:要被釋放的物件
函式返回:

1:成功
說明:

釋放掉json_object記憶體空間

json c 0 12 使用介紹

常用函式 1 json結構體初始化 struct json object json object new object 2 向json結構體中新增key value對 void json object object add struct json object dest,const char key...

json C使用小結

json c 0.7庫使用小結 json c這個庫 可以很方便的生成js的json字串.主要幾個函式如下 json object to file filepath,json object 將json object寫到檔案中 json object from file filepath 從檔案中讀出j...

json c 庫的使用方法

引入json c的庫 json c的讀寫操作 讀json c json object pobj null pobj json object from file bob.json 讀檔案裡的資料 pval json object object get jobj,sname type json obje...