json-c序列化的使用

json是什么及json的一些实例

json 是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器的解析和生成。

json的实例是键值对的形式,可以类比于c++的map

{"name":"Jack","sex":"man"}

  {"name":"Jack","age":18,"address":{"country":"china","zip-code":"10000"}}  //数字可以不加双引号

  {"a":1,"b":[1,2,3]} //数组类型

json-c库API的使用

json对象和字符串之间的相互转换

#include <head.h>
#include <json-c/json.h>
int main()
{
    const char *str = "{\"name\":\"jack\",\"age\":18,\"sex\":\"man\"}";
    //把符合json格式的字符串转换成字符对象
    struct json_object *obj = json_tokener_parse(str);
    //解析  把json对象转换为字符串
    printf("%s\n",json_object_to_json_string(obj));
    return 0;
}

创建json对象并添加内容、解析json对象并获取内容

#include <head.h>
#include <json-c/json.h>

int main()
{
    //创建空的json对象
    struct json_object *obj = json_object_new_object();
    //往json对象中添加键值对 ,把值也当做一个json对象来添加
    json_object_object_add(obj,"name",json_object_new_string("jack"));
    json_object_object_add(obj,"age",json_object_new_int(18));
    json_object_object_add(obj,"sex",json_object_new_string("man"));

    //打印json
    printf("%s\n",json_object_to_json_string(obj));
    

    //根据键名解析出json对象
    struct json_object *json;
    json_object_object_get_ex(obj,"name",&json);

    //根据json类型转换为对应的数据
    //获取json对象类型
    json_type type = json_object_get_type(json);
    if(type==json_type_string) 
    {
        printf("name: %s\n",json_object_get_string(json));
    }
    json_object_object_get_ex(obj,"age",&json);
    json_type type1 = json_object_get_type(json);
    if(type1==json_type_int)
    {
        printf("age: %d\n",json_object_get_int(json));

    }

    json_object_object_get_ex(obj,"sex",&json);
    printf("sex: %s\n",json_object_get_string(json));

    return 0;
}

数组对象的添加和解析

#include <head.h>
#include <json-c/json.h>

int main()
{
    struct json_object *obj = json_object_new_object();

    json_object_object_add(obj,"name",json_object_new_string("jack"));
    //创建json数组对象
    struct json_object *array = json_object_new_array();
    json_object_array_add(array,json_object_new_int(100));
    json_object_array_add(array,json_object_new_int(90));
    json_object_array_add(array,json_object_new_int(80));

    //把数组对象添加到json对象中
    json_object_object_add(obj,"score",array);

    printf("%s\n",json_object_to_json_string(obj));
    
    struct json_object *json;
    json_object_object_get_ex(obj,"score",&json);
    if(json_object_get_type(json)==json_type_array)
    {
        int size = json_object_array_length(json);
        for(int i=0;i<size;++i)
        {
            struct json_object *j = json_object_array_get_idx(json,i);
            if(json_type_int==json_object_get_type(j))
            {
                printf("%d\n",json_object_get_int(j));
            }
        }
    }

    return 0;
}

网络中传输数据

暂时不写通信客户端和服务端了

客户端创建发送

  //创建空的json对象
    struct json_object *obj = json_object_new_object();
    //往json对象中添加键值对 ,把值也当做一个json对象来添加
    json_object_object_add(obj,"name",json_object_new_string("jack"));
    json_object_object_add(obj,"age",json_object_new_int(18));
    json_object_object_add(obj,"sex",json_object_new_string("man"));
   //转换成buf 通过send可直接发送
    const char *buf = json_object_to_json_string(obj);
    send(socketFd,buf,strlen(buf),0);

服务端接收解析

char *buf = (char*)malloc(sizeof(char)*1024);
recv(fd,buf,1024,0);
struct json_object *obj = json_tokener_parse(buf);
struct json_object *json;
json_object_object_get_ex(obj,"name",&json);

//根据json类型转换为对应的数据
//获取json对象类型
json_type type = json_object_get_type(json);
if(type==json_type_string) 
{
    printf("name: %s\n",json_object_get_string(json));
}
json_object_object_get_ex(obj,"age",&json);
json_type type1 = json_object_get_type(json);
if(type1==json_type_int)
{
    printf("age: %d\n",json_object_get_int(json));

}

json_object_object_get_ex(obj,"sex",&json);
printf("sex: %s\n",json_object_get_string(json));

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值