1、json使用手册
json: 为了避免不同平台下的字节对齐、类型大小不统一的问题,json库把数据封装成具有一定格式的字符流数据,进行传输。
json格式:把数据与键值一一对应,数据传输双方约定好同一键值,
使用接口API根据键值操作jason对象(jason_object)存储或取得数据。
一般使用:
数据-》(封装)jason对象-》String格式-》。。。传输。。。-》String格式-》(解析)jason对象-》取得数据
(int、char..)数据,与键值成对存入json对象—————————————————————————————————>通过键值从json对象取得数据
json接口API(注意:在json中所有数据类型(arry、int、string、char)都是一个jason对象)
1、数据的封装(单对象(int、char、string)和数组(arry))
(1)新建对象:
A.创建一个Json对象:
struct json_object * json_object_new_object (void)
B.创建一个Json数组对象:
struct json_object * json_object_new_array (void)
C.销毁json对象
void json_object_put (struct json_object *obj)
(2)json对象的转换(普通类型->json对象):
1:struct json_object * json_object_new_int (int i)
2:struct json_object * json_object_new_double (double d)
3:struct json_object * json_object_new_string (const char *s)
4:struct json_object * json_object_new_boolean (boolean b)
5:struct json_object * json_object_new_string_len (const char *s, int len)
(3)json对象的处理
A.普通对象
添加:void json_object_object_add (struct json_object *obj, const char *key, struct json_object *val)
删除:void json_object_object_del (struct json_object *obj, const char *key)
查询:struct json_object * json_object_object_get (struct json_object *obj, const char *key)
根据key获取:struct json_object * json_object_object_get (struct json_object *obj, const char *key)
B.数组对象
获取长度:int json_object_array_length (struct json_object *obj)
添加:int json_object_array_add (struct json_object *obj, struct json_object *val)
指定位置添加:int json_object_array_put_idx (struct json_object *obj, int idx, struct json_object *val)
获取指定位置对象:struct json_object * json_object_array_get_idx (struct json_object *obj, int idx)
(4)json_object To 字符流
const char * json_object_to_json_string (struct json_object *obj)
1、数据的解析(解析获取到的json格式字符流)
(1)字符流 To json_object
struct json_object* json_tokener_parse(const char *str)
(2)对象获取
A.普通对象
根据key获取:struct json_object * json_object_object_get (struct json_object *obj, const char *key)
B.数组对象
获取指定位置对象:struct json_object * json_object_array_get_idx (struct json_object *obj, int idx)
(3)对象的转换(数据还原)
bool型:boolean json_object_get_boolean (struct json_object *obj)
double型:double json_object_get_double (struct json_object *obj)
整型:int json_object_get_int (struct json_object *obj)
字符数组:const char * json_object_get_string (struct json_object *obj)
2、实例代码
#include <stdio.h>
#include <json/json.h>
#include <string.h>
struct stu{
int id;
char username[20];
char passwd[20];
};
int main(void){
struct stu ss1={1,"tonly","123"};
struct stu ss2={2,"pengyuyan","12345"};
struct stu ss3={3,"wuyanzu","123456"};
struct stu arrs[3]={ss1,ss2,ss3};
//创建一个数组对象
struct json_object * obj_arr=json_object_new_array ();
int i;
struct json_object *objects;
struct json_object *obj_id;
struct json_object *obj_username;
struct json_object *obj_passwd;
for(i=0;i<3;i++){
//创建大对象
objects=json_object_new_object ();
//将数据打包成json对象数据
obj_id=json_object_new_int (arrs[i].id);
obj_username=json_object_new_string (arrs[i].username);
obj_passwd=json_object_new_string (arrs[i].passwd);
//将json数据放入大对象中
json_object_object_add (objects,"id",obj_id);
json_object_object_add (objects,"username",obj_username);
json_object_object_add (objects,"passwd",obj_passwd);
//将大对象放入数组对象中
json_object_array_add (obj_arr,objects);
}
//把数组包转换成字符串
const char *p=json_object_to_json_string (obj_arr);
printf("%s ",p);
printf("\n");
//发送成功
//解包
//把字符串转换成数组包
struct json_object* obj_arrs=json_tokener_parse(p);
//获取数组包的数据个数
int len=json_object_array_length (obj_arrs);
int ii;
struct json_object *objectss;
struct json_object *obj_ids;
struct json_object *obj_usernames;
struct json_object *obj_passwds;
for(ii=0;ii<len;ii++){
objectss=json_object_array_get_idx (obj_arrs,ii);
//通过标签获得小包
//把标签为id的json对象从大对象中拿出
obj_ids=json_object_object_get (objectss, "id");
obj_usernames=json_object_object_get (objectss, "username");
obj_passwds=json_object_object_get (objectss, "passwd");
//解析小包
printf("id:%d ",json_object_get_int (obj_ids));
printf("username:%s ",json_object_get_string (obj_usernames));
printf("passwd:%s ",json_object_get_string (obj_passwds));
printf("\n");
}
return 0;
}