建设部网站官网查询,小型网站建设公司价格,开发网站嵌入广告,首页风格一、json-c库的安装(ubuntu)
root用户运行以下命令#xff1a;
apt-get install libjson0-dev libjson0非root用户运行以下命令#xff1a;
sudo apt-get install libjson0-dev libjson0二、解析json数据
1. json_object
json_object是JSON-C库中定义的一个结构体#…一、json-c库的安装(ubuntu)
root用户运行以下命令
apt-get install libjson0-dev libjson0非root用户运行以下命令
sudo apt-get install libjson0-dev libjson0二、解析json数据
1. json_object
json_object是JSON-C库中定义的一个结构体用于表示JSON对象。 它的内部定义如下
struct json_object {enum json_type o_type; // JSON对象的类型union data {boolean c_boolean; // 布尔类型的值double c_double; // 双精度浮点数类型的值int c_int; // 整数类型的值struct lh_table* c_object; // 对象类型的值struct array_list* c_array; // 数组类型的值char* c_string; // 字符串类型的值} o;
};2.json_tokener_parse
函数原型
struct json_object* json_tokener_parse(const char* str);参数
str要解析的 JSON 字符串。
返回值
如果解析成功返回一个指向 json_object 结构体的指针表示解析后的 JSON 对象。如果解析失败返回 NULL。
3.代码实例
#include stdio.h
#include json-c/json.h
#include string.hint main()
{const char *str {\name\:\jack\,\age\:\22\,\sex\:\male\};//把符合json格式的字符串转换成json对象struct json_object *obj json_tokener_parse(str);//把json对象转换成字符串printf(%s\n, json_object_to_json_string(obj));return 0;
}三、创建json对象和添加json对象string、int和访问json对象
1. json_object_new_object
在 json-c 库中json_object_new_object 函数用于创建一个空的 JSON 对象。 函数原型
struct json_object* json_object_new_object(void);返回值
返回一个指向 json_object 结构体的指针表示创建的空 JSON 对象。
2.json_object_object_add
在 json-c 库中json_object_object_add 函数用于向 JSON 对象中添加一个键值对。 以下是该函数的用法
int json_object_object_add(struct json_object* obj, const char* key, struct json_object* val);
_参数 参数
obj要添加键值对的 JSON 对象。key要添加的键以字符串形式表示。val要添加的值可以是任何 JSON 数据类型的对象。
返回值
如果成功添加键值对返回 0。如果出现错误返回 -1。
3.json_object_object_get_ex
在 json-c 库中json_object_object_get_ex 函数用于从 JSON 对象中获取指定键的值并将其存储在提供的变量中。 以下是该函数的用法
int json_object_object_get_ex(const struct json_object* obj, const char* key, struct json_object** val);参数
obj要从中获取值的 JSON 对象。key要获取值的键以字符串形式表示。val用于存储获取到的值的指针。
返回值
如果找到指定键的值并成功存储在 val 指针中返回 1。如果未找到指定键的值返回 0。
使用 json_object_object_get_ex 函数可以从 JSON 对象中获取指定键的值并将其存储在提供的变量中。你需要传递要获取值的 JSON 对象指针 obj、要获取值的键的字符串表示 key以及一个指向 json_object* 类型的指针 val用于存储获取到的值。
4.json_object_get_type
在 json-c 库中json_object_get_type 函数用于获取 JSON 对象的数据类型。 以下是该函数的用法
enum json_type json_object_get_type(const struct json_object* obj);参数
obj要获取数据类型的 JSON 对象。
返回值“ 返回一个 enum json_type 枚举类型的值表示 JSON 对象的数据类型。json_type 枚举类型定义了不同的 JSON 数据类型如下所示
json_type_null表示 JSON 对象是空值null。json_type_boolean表示 JSON 对象是布尔值true 或 false。json_type_double表示 JSON 对象是双精度浮点数。json_type_int表示 JSON 对象是整数。json_type_object表示 JSON 对象是一个对象。json_type_array表示 JSON 对象是一个数组。json_type_string表示 JSON 对象是一个字符串。
使用 json_object_get_type 函数可以获取 JSON 对象的数据类型。你需要传递要获取数据类型的 JSON 对象指针 obj然后函数将返回对应的 enum json_type 枚举值。
5.json_object_get_string
json_object_get_string 是 json-c 库中的一个函数用于获取 JSON 对象中字符串类型的值。 以下是该函数的用法
const char* json_object_get_string(const struct json_object* obj);参数
obj要获取字符串值的 JSON 对象。
返回值
返回一个指向字符串的指针表示 JSON 对象中的字符串值。如果 JSON 对象不是字符串类型或者出现错误返回 NULL。
6.json_object_get_int
json_object_get_int 是 json-c 库中的一个函数用于获取 JSON 对象中整数类型的值。 以下是该函数的用法
int json_object_get_int(const struct json_object* obj);参数
obj要获取整数值的 JSON 对象。
返回值
返回 JSON 对象中的整数值。如果 JSON 对象不是整数类型或者出现错误返回 0。
使用 json_object_get_int 函数可以获取 JSON 对象中整数类型的值。你需要传递要获取整数值的 JSON 对象指针 obj然后函数将返回该整数值。
7.代码实例
#include stdio.h
#include json-c/json.h
#include string.hint main()
{//创建json对象struct json_object *obj json_object_new_object();//往json对象添加键值对json_object_new_string把字符串转换成json对象json_object_object_add(obj, name, json_object_new_string(jack));json_object_object_add(obj, age, json_object_new_int(22));json_object_object_add(obj, sex, json_object_new_string(male));//打印json对象printf(%s\n, json_object_to_json_string(obj));printf(%ld\n, strlen(json_object_to_json_string(obj)));//解析json//第一步根据健名解析出对应的json对象struct json_object *json;json_object_object_get_ex(obj, name, json);//第二步根据json对象类型转换成对应的数据//先获取json对象类型json_type type json_object_get_type(json);if(json_type_string type) //判断类型释放一致printf(name: %s\n, json_object_get_string(json)); //将json对象转换成字符串类型json_object_object_get_ex(obj, age, json);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));printf(sex: %s\n, json_object_get_string(json));return 0;
}
四、json数组的添加于解析
1.json_object_new_array
json_object_new_array 是 json-c 库中的一个函数用于创建一个新的 JSON 数组对象。 以下是该函数的用法
struct json_object* json_object_new_array(void);返回值
返回一个指向新创建的 JSON 数组对象的指针。
使用 json_object_new_array 函数可以创建一个新的空的 JSON 数组对象。函数将返回一个指向该数组对象的指针你可以使用这个指针进行后续的操作如向数组中添加元素。
2.json_object_array_length
json_object_array_length 是 json-c 库中的一个函数用于获取 JSON 数组对象中元素的个数。 以下是该函数的用法
int json_object_array_length(const struct json_object* obj);参数
obj要获取元素个数的 JSON 数组对象。
返回值
返回 JSON 数组对象中元素的个数。如果 obj 不是一个 JSON 数组对象返回 0。
使用 json_object_array_length 函数可以获取 JSON 数组对象中元素的个数。你需要传递要获取元素个数的 JSON 数组对象指针 obj然后函数将返回该数组对象中元素的个数。
3.json_object_array_get_idx
json_object_array_get_idx 是 json-c 库中的一个函数用于获取 JSON 数组对象中指定索引位置的元素。 以下是该函数的用法
struct json_object* json_object_array_get_idx(const struct json_object* obj, int idx);参数
obj要获取元素的 JSON 数组对象。idx要获取元素的索引位置。
返回值
返回 JSON 数组对象中指定索引位置的元素的指针。如果 obj 不是一个 JSON 数组对象或者索引越界返回 NULL。
使用 json_object_array_get_idx 函数可以获取 JSON 数组对象中指定索引位置的元素。你需要传递要获取元素的 JSON 数组对象指针 obj以及要获取元素的索引位置 idx。函数将返回指定索引位置的元素的指针。
4. 代码实例
#include stdio.h
#include json-c/json.h
#include string.hint 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); //获取json_type_array的长度for(int i 0; i size; i){struct json_object *j json_object_array_get_idx(json, i); //根据下标提取json对象if(json_type_int json_object_get_type(j))printf(%d\n, json_object_get_int(j));}} return 0;
}
五、json-c的TCP实例
1.服务器
#include stdio.h
#include sys/types.h /* See NOTES */
#include sys/socket.h
#include netinet/in.h
#include arpa/inet.h
#include string.h
#include stdlib.h
#include unistd.h
#include json-c/json.hint main()
{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(127.0.0.1);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(3);}printf(接受客户端 %d 的连接\n, fd);char buf[1024] {0};int size recv(fd, buf, 1024, 0);if(-1 size){perror(recv);exit(4);}//把字符串转换成jsonstruct 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));json_object_object_get_ex(obj, sex, json);printf(sex : %s\n, json_object_get_string(json));close(fd);close(sockfd);
}
2.客户端
#include stdio.h
#include sys/types.h /* See NOTES */
#include sys/socket.h
#include netinet/in.h
#include arpa/inet.h
#include stdlib.h
#include string.h
#include unistd.h
#include json-c/json.hint main()
{int sockfd socket(AF_INET, SOCK_STREAM, 0); if(-1 sockfd){ perror(socket);exit(1);} struct sockaddr_in server_info;bzero(server_info, 0); server_info.sin_family AF_INET;server_info.sin_port 7000;server_info.sin_addr.s_addr inet_addr(127.0.0.1);if(connect(sockfd, (struct sockaddr *)server_info, sizeof(server_info)) -1) { perror(connect);exit(2);} struct json_object *json json_object_new_object();json_object_object_add(json, name, json_object_new_string(jack));json_object_object_add(json, age, json_object_new_int(24));json_object_object_add(json, sex, json_object_new_string(male));const char *buf json_object_to_json_string(json);if(send(sockfd, buf, strlen(buf), 0) -1){perror(send);exit(3);}printf(字符串 %s 发送成功长度%ld\n, buf, strlen(buf));close(sockfd);return 0;
}