json
json 格式有两种
1: 键 -- 值 的集合 抽象为对象 object
例如:{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" }
2: 有序的列表 抽象为数组 array
例如:
{ "people": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb"},
{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
]
}
注意数组元素之间用 “逗号” 隔开,且最后一个元素无 “逗号”
json_type_object 键--值 对的集合
json对象值的类型:
json_type_boolean
json_type_double,
json_type_int,
json_type_array, “值”的集合
json_type_string
API接口函数:
struct json_object *json_object_new_object();
创建一个 struct json_object 类型的json对象
struct json_object* json_object_new_boolean(boolean b);
创建一个 struct json_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)
数组值类型的集合:
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字符串
struct json_object *json_object_object_get(struct json_object *json,char *name);
从json中按名字取一个对象
参数:json ----- json对象
name ----- json域名字
struct json_object * * json_object_get(struct json_object * this)
参数: this ----- json对象
Void json_object_put(struct json_object * this)
释放对象引用次数一次
例子:
---------------------------------------------------------------------------------------
my_string = json_object_new_string("\t");
printf("my_string=%s\n", json_object_get_string(my_string));
printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
json_object_put(my_string);
---------------------------------------------------------------------------------------
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_object 实例
char * json_object_to_json_string(struct json_object * this)
将json_object内容转换json格式字符串,其中可能含有转义符
参数: this ----- json_object 实例
返回值: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对象
参数:同上
int json_object_array_length(struct json_object *obj);
获得json对象数组的长度
参数:obj —— json数组值对象
extern int json_object_array_add(struct json_object *obj,struct json_object *val);
添加一元素在json数组对象末端
参数:obj —— json数组值对象
val —— json值对象
int json_object_array_put_idx(struct json_object *obj, int idx,struct json_object *val);
在指定的json数组对象下标插入或替换一个json对象元素
参数:obj —— json数组值对象
val —— json值对象
idx —— 数组下标
struct json_object * json_object_array_get_idx(struct json_object * json_array,int i)
从数组中按下标取json值对象
参数:json_array ---- 数组值对象
i ---- 数组下标位置
定义宏 json_object_object_foreach(obj,key,val)
参数: 遍历json对象的key和值