当前位置: 首页 > 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/14347750/

相关文章:

  • 杨凌企业网站建设wordpress 说说
  • 个人网站备案涉及支付宝建站工具 风铃
  • H5网站模板修改教程建设银行网站为什么进不去
  • 广东营销型网站建设报价海创网站建设
  • 个人网站制作成品图片流量套餐网站
  • 网站关键词格式钦州做网站的公司
  • drupal做的网站农家乐网站建设方案
  • 政务公开系统网站建设第三方做农产品价格数据的网站
  • 百度站长平台网站体检做个网站做什么呢
  • 网站抓取超时错误手机网站栏目结构图
  • 北京网站建立网站怎么做pc端盒子
  • 北京市建设官方网站小程序询价表
  • 网站建设支付深圳出台科技支持政策
  • 建设企业网站个人网上有几家做网站的公司好
  • 如何快速提升网站关键词排名wordpress版本控制
  • 济南腾飞网络网站建设六安人
  • 云虚拟主机怎么做网站下载软件的app大全
  • 百度一下你就知道官网首页免费优化推广网站的软件
  • 免费网站建设排行成都建设网站的公司有哪些
  • 备案号如何绑定多个网站wordpress 注册角色
  • 松门建设规划局网站海南茶叶网站建设
  • 设计营销型网站域名二级黄冈站
  • 重庆网站建设电话网络营销的特点包括超前性
  • 网站建设的需求要素h5网站制作接单
  • 建站之星换模板网站地图怎么建设
  • 法人变更在哪个网站做公示杂志网站建设
  • 论坛类网站如何备案十堰微网站建设价格
  • 微小旅行社能否做网站wordpress 幻灯片无法显示
  • 做网站都能用什么做网站建设管理工作交流发言材料
  • 深圳市建设监理协会网站北京制作手机网站