当前位置: 首页 > news >正文

建设部网站官网查询小型网站建设公司价格

建设部网站官网查询,小型网站建设公司价格,开发网站嵌入广告,首页风格一、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; }
http://www.hkea.cn/news/14300075/

相关文章:

  • 找工作哪个网站好找网站运营专员做什么
  • 网站相册代码画册设计赏析
  • 宁波建网站价格海尔公司网站建设现状
  • 哈尔滨网站建设制作哪家好一个网站seo做哪些工作内容
  • 找网站开发项目做自媒体需要哪些网站
  • 想学做网站学什么教程怎样制定一个网站建设方案
  • 做动态图网站违法吗小程序搭建公司
  • 邢台做网站优化价格租服务器发布网站
  • 合肥市有做网站的公司吗商丘做网站优化
  • 如何做网站的网页火车头wordpress 4.7
  • 娄底高端网站建设wordpress 登录后跳转
  • 做h5网站设计设计公司团队
  • 中小型电子商务网站有哪些wordpress用户组阅读文章
  • 甘孜州手机网站建设安装wordpress要数据库
  • 织梦网站文章相互调用如何能查到百度搜索排名
  • 大学英文网站建设举措绍兴网站制作
  • 爱站权重建设什么网站
  • 旅游网站建设目标分析公司简介范本
  • 东莞网站建设推广公司精美ppt模板免费下载完整版
  • 个人可以架设网站吗内容营销的方式
  • 成立网站要什么手续建网站需要备案吗
  • 阿里云服务器建设两个网站ftp制作网站
  • 专业网站制作仪表多少钱建设旅游网站的好处
  • 河南企业网站制作网站建设费属于研发费用吗
  • 网站建设先做后泰州手机网站制作
  • 企业网站seo怎么做滑县网站建设服务
  • 黑龙江建设厅网站首页网站切图大图
  • 广告网站建设与制作wordpress $_SERVER
  • 装修公司做网销的网站免费网页制作
  • 做网站都需要买什么协会网站建设需求文档