json-c序列化的使用

json-c序列化的使用,第1张

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

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

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("%sn",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: %sn",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: %dn",json_object_get_int(json));

    }

    json_object_object_get_ex(obj,"sex",&json);
    printf("sex: %sn",json_object_get_string(json));

    return 0;
}

数组对象的添加和解析
#include 
#include 

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("%sn",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 
网络中传输数据 
暂时不写通信客户端和服务端了 
客户端创建发送 
  //创建空的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: %sn",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: %dn",json_object_get_int(json));

}

json_object_object_get_ex(obj,"sex",&json);
printf("sex: %sn",json_object_get_string(json));

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zaji/5593429.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存