【json打包和解包】

//打包
const char * json_package(msg datapack)
{
//第一步
struct json_object *obj = json_object_new_array();

//第二步 
struct json_object *str1 = json_object_new_object();
struct json_object *str2 = json_object_new_object();
struct json_object *str3 = json_object_new_object();
struct json_object *str4 = json_object_new_object();
struct json_object *str5 = json_object_new_object();
struct json_object *str6 = json_object_new_object();
struct json_object *str7 = json_object_new_object();

//第三步 
struct json_object *value1 = json_object_new_int(datapack.cmd);
struct json_object *value2 = json_object_new_string(datapack.name);
struct json_object *value3 = json_object_new_string(datapack.password);
struct json_object *value4 = json_object_new_int(datapack.user_qq);
struct json_object *value5 = json_object_new_int(datapack.user_qq_to);
struct json_object *value6 = json_object_new_int(datapack.group_qq);
struct json_object *value7 = json_object_new_string(datapack.buf);

//第四步
json_object_object_add(str1,"cmd",value1);
json_object_object_add(str2,"name",value2);
json_object_object_add(str3,"password",value3);
json_object_object_add(str4,"user_qq",value4);
json_object_object_add(str5,"user_qq_to",value5);
json_object_object_add(str6,"group_qq",value6);
json_object_object_add(str7,"buf",value7);

// 第五步
json_object_array_add(obj, str1);
json_object_array_add(obj, str2);
json_object_array_add(obj, str3);
json_object_array_add(obj, str4);
json_object_array_add(obj, str5);
json_object_array_add(obj, str6);
json_object_array_add(obj, str7);

//第六步
const char *p = json_object_to_json_string(obj);
return p;

}

//json解包
msg json_unpack(const char *p)
{
//第一步 将字符流转换成大容器
struct json_object *obj = json_tokener_parse§;

//第二步 获取数组中指定位置的对象 就是获取两个小容器
struct json_object *str1 = json_object_array_get_idx(obj,0);
struct json_object *str2 = json_object_array_get_idx(obj,1);
struct json_object *str3 = json_object_array_get_idx(obj,2);
struct json_object *str4 = json_object_array_get_idx(obj,3);
struct json_object *str5 = json_object_array_get_idx(obj,4);
struct json_object *str6 = json_object_array_get_idx(obj,5);
struct json_object *str7 = json_object_array_get_idx(obj,6);

//第三步 根据小容器中的key值获取数值对象
struct json_object *value1 = json_object_object_get(str1,"cmd");
struct json_object *value2 = json_object_object_get(str2,"name");
struct json_object *value3 = json_object_object_get(str3,"password");
struct json_object *value4 = json_object_object_get(str4,"user_qq");
struct json_object *value5 = json_object_object_get(str5,"user_qq_to");
struct json_object *value6 = json_object_object_get(str6,"group_qq");
struct json_object *value7 = json_object_object_get(str7,"buf");

//第四步 将数值对象转换成对应的数值
int cmd = json_object_get_int(value1);
const char *name = json_object_get_string(value2);
const char *password = json_object_get_string(value3);
int user_qq = json_object_get_int(value4);
int user_qq_to = json_object_get_int(value5);
int group_qq = json_object_get_int(value6);
const char *buf = json_object_get_string(value7);

//结构体打包
msg datapack;
datapack.cmd = cmd;
strcpy(datapack.name, name);
strcpy(datapack.password, password);
datapack.user_qq = user_qq;
datapack.user_qq_to = user_qq_to;
datapack.group_qq = group_qq;
strcpy(datapack.buf, buf);

return datapack;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值