C:JSON-C简介

介绍

        JSON-C是一个用于处理JSON格式数据的C语言库,提供了一系列操作JSON数据的函数。

一、json参数类型

typedef enum json_type {
    json_type_null,
    json_type_boolean,
    json_type_double,
    json_type_int,
    json_type_object,
    json_type_array,
    json_type_string,
} json_type;

二、json API函数

以下是JSON-C中常用的函数介绍:

  • json_object_new_object

创建一个新的json_object对象。

struct json_object* json_object_new_object(void);

  • json_object_object_add

往json对象中添加键值对。

void json_object_object_add(struct json_object *obj, const *key, struct json_object *value);
//obj:要被添加的json_object对象
//key:字段名称
//val:与key关联的json_object对象

  • json_object_new_int

将整数转换成json格式的对象。

struct json_object* json_object_new_int(int32_t i);

  • json_object_get_int

获取json对象的整形数值。

int32_t json_object_get_int(struct json_object *obj);

  • json_object_new_array

创建一个类型为array的json_object新对象。

struct json_object* json_object_new_array(void);

  • json_object_array_add

将json_object对象val添加到obj中。

int json_object_array_add(struct json_object *obj, struct json_object *val);
//obj:要被添加的json_object对象
//val:与添加到obj的json_object对象

  •  json_object_get_array

返回JSON对象的数组值。

struct array_list *json_object_get_array(json_object *obj);

  • json_object_array_length

返回JSON数组中的元素数量。 

int json_object_array_length(json_object *array);

  • json_object_new_string

创建一个类型为string的json_object新对象。

struct json_object* json_object_new_string(const char *s);

  • json_object_get_string

获取json对象的字符串值。

const char *json_object_get_string(struct json_object *obj);

  • json_object_to_file

将JSON对象写入文件中。

int json_object_to_file(char *filename, json_object *obj);

  • json_tokener_parse

将符合json格式的str字符串构造成一个json对象。

struct json_object* json_tokener_parse(const char *str);
//str:包含json数据的字符串

  •  json_object_object_get

 获取JSON对象中给定键的值。

json_object *json_object_object_get(json_object *obj, const char *key)

  • json_object_object_get_ex

从obj中获取字段为key的json_object对象指针val。(返回值为是否查询到对应键值)

json_bool json_object_object_get_ex(struct json_object* obj, const char *key, struct json_object **value);
//obj:json_object对象
//key:要获取的字段名称
//val:与key关联的json_object对象

  • json_object_get_type

获取JSON对象的类型。

enum json_type json_object_get_type(struct json_object *obj);
//返回类型:
//json_type_null
//json_type_boolean
//json_type_double
//json_type_int
//json_type_object
//json_type_array
//json_type_string

  • json_object_to_json_string

将json对象内容,转成json格式的字符串格式。

const char* json_object_to_json_string(struct json_object *obj);

  • json_object_get

用于获取JSON对象的引用计数,每当引用计数增加一次时,需要调用该函数。它的作用是将JSON对象的引用计数加1,表示有一个新的指针指向该对象。

struct json_object *json_object_get(struct json_object *obj);

  • json_object_put

用于释放JSON对象占用的内存。每当引用计数减少到0时,需要调用该函数。它的作用是将JSON对象的引用计数减1,如果引用计数为0,则释放该对象的内存。

void json_object_put(struct json_object *obj);

注意:在使用json_object_get函数增加引用计数后,必须要对相应的JSON对象使用json_object_put函数释放引用计数,否则会造成内存泄漏。

三、例子

demo案例1(字符串、数字)

#include 
#include 
#include 
#include 
 
int main(void) {
    //一、创建JSON
    // 1、创建空json对象
    struct json_object *obj = json_object_new_object();
 
    // 2、往json对象里添加键值对
    json_object_object_add(obj, "name", json_object_new_string("xiaoming"));
    json_object_object_add(obj, "age", json_object_new_int(20));
 
    // 3、打印json对象的内容和长度
    printf("%s\n", json_object_to_json_string(obj));
    printf("%ld\n", strlen(json_object_to_json_string(obj)));
 
    //二、解析JSON
    // 1:根据键名解析出对应的json对象
    struct json_object *json;
    json_object_object_get_ex(obj, "name", &json);
 
    // 2:根据json对象类型转换成对应的数据
    json_type type = json_object_get_type(json);// 先获取json对象类型
    if (json_type_string == type) {
        printf("name: %s\n", json_object_get_string(json));
    }
 
    json_object_object_get_ex(obj, "age", &json);
    printf("age: %d\n", json_object_get_int(json));
 
    return 0;
}	

demo案例2(字符串、数组)

#include 
#include 
 
int main(void) {
    struct json_object *obj = json_object_new_object();
 
    json_object_object_add(obj, "name", json_object_new_string("xiaoming"));
 
    // 创建json数组对象
    struct json_object *array = json_object_new_array();
    json_object_array_add(array, json_object_new_int(99));
    json_object_array_add(array, json_object_new_int(100));
 
    // 把数组对象添加到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 i;
        // 获取json_type_array类型json对象长度
        int size = json_object_array_length(json);
        for (i = 0; i < size; i++) {
            // 根据下标提取json对象
            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;
}

demo案例3(服务端-客户端之间发送json消息)

server侧

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
int main(void) {
    //第一步:创建socket  
    // socket():地址族:IPV4协议  套接字类型:流式套接字
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (-1 == sockfd) {
        perror("socket");
        exit(1);
    }
 
    // 第二步:绑定信息
    struct sockaddr_in server_info;  // 用于保存服务器的信息(IP PORT)
    bzero(&server_info, sizeof(struct sockaddr_in)); // 清空
    server_info.sin_family = AF_INET; // 地址族
    server_info.sin_port = 7000; // 端口 大于1024都行
    //server_info.sin_addr.s_addr = 127.0.0.1; // 表示回环IP 用于测试
    server_info.sin_addr.s_addr = inet_addr("192.168.1.100");
 
    if (bind(sockfd, (struct sockaddr *)&server_info,
        sizeof(server_info)) == -1) {
        perror("bind");
        exit(2);
    }
 
    // 第三步:设置监听队列
    if (listen(sockfd, 10) == -1) {
        perror("listen");
        exit(3);
    }
 
    printf("等待客户端的连接 ...\n");
 
    // 第四步:接受连接 (阻塞)
    struct sockaddr_in client_info; // 用于保存客户端的信息
    int length = sizeof(client_info);
 
    int fd = accept(sockfd, (struct sockaddr *)&client_info, &length);
    if (-1 == fd) {
        perror("accept");
        exit(4);
    }
 
    printf("接受客户端的连接 %d \n", fd);
    
    // 第五步:读写操作
    char buf[1024] = {0};
    ssize_t size;
 
    size = recv(fd, buf, sizeof(buf), 0);
    if (-1 == size) {
        perror("recv");
        exit(5);
    }
 
    // 字符串转换成json
    struct json_object *obj = json_tokener_parse(buf);
    struct json_object *json;
 
    json_object_object_get_ex(obj, "name", &json);
    printf("name: %s\n", json_object_get_string(json));
 
    json_object_object_get_ex(obj, "age", &json);
    printf("age: %d\n", json_object_get_int(json));
 
    close(fd);  // 关闭TCP连接,不能再接收数据
    close(sockfd); // 关闭socket,不能再处理客户端的请求
 
    // sockfd用于处理客户端连接  fd用于处理客户端的消息
    return 0;
}       

client

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
 
int main(void) {
    // 第一步:创建socket
    int sockfd = socket(AF_INET, SOCK_STREAM, 0); 
    if (-1 == sockfd) {
        perror("socket");
        exit(1);
    }   
 
    // 第二步:发起连接
    struct sockaddr_in server_info; // 保存服务器的信息
    bzero(&server_info, sizeof(server_info));
    server_info.sin_family = AF_INET;
    server_info.sin_port = 7000;
    server_info.sin_addr.s_addr = inet_addr("192.168.1.100");
 
    if (-1 == connect(sockfd, (struct sockaddr *)&server_info, 
            sizeof(server_info))) {
        perror("connect");
        exit(2);
    }
 
    // 第三步:读写数据
    struct json_object *json = json_object_new_object();
    json_object_object_add(json, "name", json_object_new_string("xiaoming"));
    json_object_object_add(json, "age", json_object_new_int(20));
 
    const char *buf = json_object_to_json_string(json);
 
    if (-1 == send(sockfd, buf, strlen(buf), 0)) {
        perror("send");
        exit(3);
    }
 
    printf("字符串 %s 发送成功 长度 %ld \n", buf,strlen(buf));
 
    close(sockfd);
 
    return 0;
}

四、更多

        如果您需要更详细的介绍或了解其他函数,建议查阅JSON-C官方文档。

你可能感兴趣的:(#,C,json)