序文:
json パッケージ化が必要な理由:
異なるプラットフォームでのバイト アライメントや型サイズの不一致の問題を回避するために、json ライブラリはデータを特定の形式の文字ストリーム データにカプセル化して送信します。
json形式:
データとキー値は 1 対 1 に対応し、データ送信の双方が同じキー値に同意し、インターフェイス API を使用してジェイソン オブジェクト (jason_object) を操作し、キー値に基づいてデータを保存または取得します。
一般的な用途:
データ --->(カプセル化された) json オブジェクト --->文字列形式 ----> ---->送信 ---->---->文字列形式 ---->(解析) 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 文字ストリームへ:
const char * json_object_to_json_string (struct json_object *obj)
2. データ分析: アンパック
(1) 文字ストリーム 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)
3. テスト
(1) まず、パッケージ化する必要がある構造体を定義します (さまざまな種類のデータ送信を容易にするためにここで使用される構造体は自分で定義できます)。個人は自分のニーズに応じてデータをパッケージ化できます。
//通信结构体
typedef struct user
{
int work; //工作指令
int flag; //反馈信息
char username[30]; //登录昵称
char passwd[30]; //登录密码
char othername[30]; //私聊对象
char msg[300]; //消息内容
int online; //在线状态
}data_t;
(2) パッケージング機能:
//打包数据-
const char * pack_user(data_t user){
//创建大对象
struct json_object* objects= json_object_new_object();
//2>将数据打包成json小对象(约定的结构体中有几个成员就打包几个小对象)
struct json_object* values1=json_object_new_int(user.work);
struct json_object* values2=json_object_new_int(user.flag);
struct json_object* values3=json_object_new_string(user.username);
struct json_object* values4=json_object_new_string(user.passwd);
struct json_object* values5=json_object_new_string(user.othername);
struct json_object* values6=json_object_new_string(user.msg);
struct json_object* values7=json_object_new_int(user.online);
//3>贴标签放入大对象
json_object_object_add (objects,"work",values1);
json_object_object_add (objects,"flag",values2);
json_object_object_add (objects,"username",values3);
json_object_object_add (objects,"passwd",values4);
json_object_object_add (objects,"othername",values5);
json_object_object_add (objects,"msg",values6);
json_object_object_add (objects,"online",values7);
//4>大对象转成字符串类型
const char *p=json_object_to_json_string(objects);
// printf("test 263\n");
char* str = malloc(500); // 动态分配内存
strcpy(str,p);
return str;
}
(3) データのアンパック機能:
data_t unpack_user( const char *js){
static data_t rslt;
//解析json,还原数据成原来的类型
struct json_object * sale_json=json_tokener_parse(js);
//根据标签key获取小对象数据
struct json_object * sale_obj1=json_object_object_get(sale_json,"work");
struct json_object * sale_obj2=json_object_object_get(sale_json,"flag");
struct json_object * sale_obj3=json_object_object_get(sale_json,"username");
struct json_object * sale_obj4=json_object_object_get(sale_json,"passwd");
struct json_object * sale_obj5=json_object_object_get(sale_json,"othername");
struct json_object * sale_obj6=json_object_object_get(sale_json,"msg");
struct json_object * sale_obj7=json_object_object_get(sale_json,"online");
//提取小对象里面的数据
rslt.work=json_object_get_int (sale_obj1);
rslt.flag=json_object_get_int (sale_obj2);
strcpy(rslt.username,json_object_get_string (sale_obj3));
//strcpy(rslt.username,wd1);
strcpy(rslt.passwd,json_object_get_string (sale_obj4));
//strcpy(rslt.passwd,wd2);
strcpy(rslt.othername,json_object_get_string (sale_obj5));
//strcpy(rslt.othername,wd3);
strcpy(rslt.msg,json_object_get_string (sale_obj6));
//strcpy(rslt.msg,wd4);
rslt.online=json_object_get_int (sale_obj7);
return rslt;
}
(4)用途:
char buf[100];
//接受打包传来的数据,放在buf里面
bzero(buf,strlen(buf));//清空buf里面的垃圾值,再接受
int ret = recv(cid,buf,sizeof(char)*1000,0);//接收数据
//将打包的数据赋值给data_t类型
data_t request = unpack_user(buf);
//将结构体打包成char*类型,通过strcpy函数赋值给p
char *p;
strcpy(p,pack_user(request)));