ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

cJSON使用方法

2021-07-06 12:04:20  阅读:249  来源: 互联网

标签:cJSON 方法 data AddStringToObject JSON result 使用 root


  1 #include <iostream>
  2 #include "cJSON.h"
  3 
  4 using namespace std;
  5 
  6 //1.一个简单的JSON键值对构建
  7 void Create_Simple_JSON(void)
  8 {
  9     cJSON *root;
 10     root = cJSON_CreateObject();//创建一个json对象
 11     cJSON_AddItemToObject(root, "CSDN", cJSON_CreateString("https://blog.csdn.net/whik1194"));
 12     cJSON_AddItemToObject(root, "cnblogs", cJSON_CreateString("https://home.cnblogs.com/u/whik/"));
 13     cJSON_AddItemToObject(root, "Github", cJSON_CreateString("https://github.com/whik/"));
 14     cJSON_AddStringToObject(root, "Blog", "http://www.wangchaochao.top/");
 15 
 16     printf("构建的JSON:\n%s\n", cJSON_Print(root));
 17     cJSON_Delete(root);
 18 }
 19 
 20 //2.键的值是一个JSON对象
 21 void Create_BJTime_JSON(void)
 22 {
 23 
 24     cJSON *root;
 25     cJSON *result;
 26 
 27     root = cJSON_CreateObject();//创建一个json对象
 28 
 29     result = cJSON_CreateObject();
 30     //result构建
 31 
 32     cJSON_AddStringToObject(result, "timestamp", "ok");
 33     cJSON_AddStringToObject(result, "datetime_1", "2019-07-21 10:46:57");
 34     cJSON_AddStringToObject(result, "datetime_2", "2019年07月21日 10时46分57秒");
 35     cJSON_AddStringToObject(result, "week_1", "0");
 36     cJSON_AddStringToObject(result, "week_2", "星期日");
 37     cJSON_AddStringToObject(result, "week_3", "周日");
 38     cJSON_AddStringToObject(result, "week_4", "Sunday");
 39 
 40     cJSON_AddStringToObject(root, "status", "success");
 41     cJSON_AddItemToObject(root, "result", result);
 42     cJSON_AddStringToObject(root, "Blog", "www.wangchaochao.top");
 43 
 44     printf("构建的JSON:\n%s\n", cJSON_Print(root));
 45     cJSON_Delete(root);
 46 }
 47 
 48 //3.JSON数组,元素是字符串
 49 void Create_Array_Str_JSON(void)
 50 {
 51     cJSON *root;
 52     const char *strings[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
 53 
 54     root=cJSON_CreateStringArray(strings,7);
 55 
 56     printf("%s\n",cJSON_Print(root));
 57     cJSON_Delete(root);
 58 }
 59 
 60 //4.键的值是一个数组,数组包含多个对象元素
 61 void Create_Array_JSON(void)
 62 {
 63     cJSON *root;
 64     cJSON *forceast;
 65     cJSON *day1, *day2, *day3;  //数组
 66 
 67     day1 = cJSON_CreateObject();
 68     day2 = cJSON_CreateObject();
 69     day3 = cJSON_CreateObject();
 70 
 71     cJSON_AddStringToObject(day1, "date", "2019-07-21");    //日期
 72     cJSON_AddStringToObject(day1, "cond_txt", "多云");      //天气状况
 73     cJSON_AddStringToObject(day1, "cond_code", "101");      //天气代码
 74     cJSON_AddStringToObject(day1, "hum", "23");             //湿度
 75     cJSON_AddStringToObject(day1, "tmp_H", "31");           //最高温度
 76     cJSON_AddStringToObject(day1, "tmp_L", "25");           //最低温度
 77 
 78     cJSON_AddStringToObject(day2, "date", "2019-07-22");
 79     cJSON_AddStringToObject(day2, "cond_txt", "晴");
 80     cJSON_AddStringToObject(day2, "cond_code", "100");
 81     cJSON_AddStringToObject(day2, "hum", "20");
 82     cJSON_AddStringToObject(day2, "tmp_H", "33");
 83     cJSON_AddStringToObject(day2, "tmp_L", "26");
 84 
 85     cJSON_AddStringToObject(day3, "date", "2019-07-23");
 86     cJSON_AddStringToObject(day3, "cond_txt", "阵雨");
 87     cJSON_AddStringToObject(day3, "cond_code", "107");
 88     cJSON_AddStringToObject(day3, "hum", "45");
 89     cJSON_AddStringToObject(day3, "tmp_H", "32");
 90     cJSON_AddStringToObject(day3, "tmp_L", "25");
 91 
 92     forceast = cJSON_CreateArray();
 93     //注意顺序,索引依次递增
 94     cJSON_AddItemToArray(forceast, day1);   //元素0
 95     cJSON_AddItemToArray(forceast, day2);   //元素1
 96     cJSON_AddItemToArray(forceast, day3);   //元素2
 97 
 98     root = cJSON_CreateObject();    //创建一个json对象
 99 
100     cJSON_AddStringToObject(root, "status", "ok");
101     cJSON_AddItemToObject(root, "weather", forceast);
102     cJSON_AddStringToObject(root, "update", "2019-07-21 11:00");
103     cJSON_AddStringToObject(root, "Blog", "www.wangchaochao.top");
104     //等效于:cJSON_AddItemToObject(root, "update", cJSON_CreateString("2019-07-21 11:00");
105 
106     printf("构建的JSON:\n%s\n", cJSON_Print(root));
107     cJSON_Delete(root);
108 }
109 
110 //5.数组内嵌套了5个数组,每个数组内有5个字符串元素
111 void Create_Array_Nest_JSON(void)
112 {
113     struct oil_stu{
114         char *city;          //城市名称
115         char *oil_92_price;  //92号汽油价格
116         char *oil_95_price;
117         char *oil_98_price;
118         char *oil_0_price;
119     };
120 
121     cJSON *root;
122     cJSON *data;  //包含多个数组
123     cJSON *table, *data_bj, *data_sh, *data_js, *data_tj;
124 
125     const char *bj_str[5] = {"北京", "6.78", "7.21", "8.19", "6.45"};
126     const char *sh_str[5] = {"上海", "6.74", "7.17", "7.87", "6.39"};
127     const char *js_str[5] = {"江苏", "6.75", "7.18", "8.06", "6.37"};
128     const char *tj_str[5] = {"天津", "6.77", "7.15", "8.07", "6.41"};
129     const char *talbe_str[5] = {"地区", "92号汽油", "95号汽油", "98号汽油", "0号柴油"};
130 
131     data_bj = cJSON_CreateStringArray(bj_str, 5);   //只包含5个字符串的数组
132     data_sh = cJSON_CreateStringArray(sh_str, 5);
133     data_js = cJSON_CreateStringArray(js_str, 5);
134     data_tj = cJSON_CreateStringArray(tj_str, 5);
135     table   = cJSON_CreateStringArray(talbe_str, 5);
136 
137     data = cJSON_CreateArray();
138     cJSON_AddItemToArray(data, table);
139     cJSON_AddItemToArray(data, data_bj);
140     cJSON_AddItemToArray(data, data_sh);
141     cJSON_AddItemToArray(data, data_js);
142     cJSON_AddItemToArray(data, data_tj);
143 
144     root = cJSON_CreateObject();
145 
146     cJSON_AddStringToObject(root, "status", "ok");
147     cJSON_AddStringToObject(root, "msg", "2019-07-21 11:00");
148     cJSON_AddStringToObject(root, "update", "2019-07-21 11:00");
149     cJSON_AddItemToObject(root, "data", data);
150     cJSON_AddStringToObject(root, "About", "wcc");
151     cJSON_AddStringToObject(root, "Blog", "www.wangchaochao.top");
152 
153     printf("构建的JSON:\n%s\n", cJSON_Print(root));
154     cJSON_Delete(root);
155 }
156 
157 int main() {
158     Create_Simple_JSON();
159     cout << endl << endl;
160     Create_BJTime_JSON();
161     cout << endl << endl;
162     Create_Array_Str_JSON();
163     cout << endl << endl;
164     Create_Array_JSON();
165     cout << endl << endl;
166     Create_Array_Nest_JSON();
167     return 0;
168 }

 

标签:cJSON,方法,data,AddStringToObject,JSON,result,使用,root
来源: https://www.cnblogs.com/yuhong1103/p/14976264.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有