加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cJSON结构体构建

发布时间:2020-12-16 18:42:31 所属栏目:百科 来源:网络整理
导读:? cJSON结构体构建 一:cJSON的构建。 1 int create_objects() 2 { 3 cJSON *root,*fmt,*img,*thm,* fld; 4 char * out ; 5 int i; /* The index number. */ 6 int ret = 0 ; 7 8 /* Here we construct several JSON objects. */ 9 10 // ------------------

?

cJSON结构体构建

一:cJSON的构建。

 1 int create_objects()
 2 {
 3     cJSON *root,*fmt,*img,*thm,*fld;
 4     char *out;
 5     int i;    /* The index number. */
 6     int ret = 0;
 7 
 8     /* Here we construct several JSON objects. */
 9 
10     // ------------------构建第1个----------------------
11     /* The "Video" data type: */
12     root = cJSON_CreateObject();
13     cJSON_AddItemToObject(root,"name",cJSON_CreateString("Jack ("Bee") Nimble"));
14     cJSON_AddItemToObject(root,"format",fmt = cJSON_CreateObject());
15     cJSON_AddStringToObject(fmt,"type","rect");
16     cJSON_AddNumberToObject(fmt,"width",1920);
17     cJSON_AddNumberToObject(fmt,"height",1080);
18     cJSON_AddFalseToObject (fmt,"interlace");
19     cJSON_AddNumberToObject(fmt,"frame rate",24);
20 
21     out = cJSON_Print(root); /* Print to text */
22     cJSON_Delete(root);      /* Delete the cJSON object */
23     LOG_I(cjson_example,"%sn",out); /* Print out the text */
24     cJSON_free(out);         /* Release the string. */
25 
26     // ------------------构建第2个----------------------
27     /* The "days of the week" array: */
28     const char *strings[7] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
29     root = cJSON_CreateStringArray(strings,7);
30 
31     out = cJSON_Print(root);
32     cJSON_Delete(root);
33     LOG_I(cjson_example,out);
34     cJSON_free(out);
35 
36      // ------------------构建第3个----------------------
37     /* The matrix: */
38     int numbers[3][3] = {{0,-1,0},{1,0,{0,1}};
39     root = cJSON_CreateArray();
40     for (i = 0; i < 3; i++) {
41         cJSON_AddItemToArray(root,cJSON_CreateIntArray(numbers[i],3));
42     }
43 
44     /*    cJSON_ReplaceItemInArray(root,1,cJSON_CreateString("Replacement")); */
45     out = cJSON_Print(root);
46     cJSON_Delete(root);
47     LOG_I(cjson_example,out);
48     cJSON_free(out);
49 
50     // ------------------构建第4个----------------------
51     /* The "gallery" item: */
52     int ids[4] = {116,943,234,38793};
53     root = cJSON_CreateObject();
54     cJSON_AddItemToObject(root,"Image",img = cJSON_CreateObject());
55     cJSON_AddNumberToObject(img,"Width",800);
56     cJSON_AddNumberToObject(img,"Height",600);
57     cJSON_AddStringToObject(img,"Title","View from 15th Floor");
58     cJSON_AddItemToObject(img,"Thumbnail",thm = cJSON_CreateObject());
59     cJSON_AddStringToObject(thm,"Url","http:/*www.example.com/image/481989943");
60     cJSON_AddNumberToObject(thm,125);
61     cJSON_AddStringToObject(thm,"100");
62     cJSON_AddItemToObject(img,"IDs",cJSON_CreateIntArray(ids,4));
63 
64     out = cJSON_Print(root);
65     cJSON_Delete(root);
66     LOG_I(cjson_example,out);
67     cJSON_free(out);
68 
69     // ------------------构建第5个----------------------
70     /* The array of "records": */
71     struct record fields[2] = {
72         {"zip",37.7668,-1.223959e+2,"","SAN FRANCISCO","CA","94107","US"},73         {"zip",37.371991,-1.22026e+2,"SUNNYVALE","94085","US"}
74     };
75 
76     root = cJSON_CreateArray();
77     for (i = 0; i < 2; i++) {
78         cJSON_AddItemToArray(root,fld = cJSON_CreateObject());
79         cJSON_AddStringToObject(fld,"precision",fields[i].precision);
80         cJSON_AddNumberToObject(fld,"Latitude",fields[i].lat);
81         cJSON_AddNumberToObject(fld,"Longitude",fields[i].lon);
82         cJSON_AddStringToObject(fld,"Address",fields[i].address);
83         cJSON_AddStringToObject(fld,"City",fields[i].city);
84         cJSON_AddStringToObject(fld,"State",fields[i].state);
85         cJSON_AddStringToObject(fld,"Zip",fields[i].zip);
86         cJSON_AddStringToObject(fld,"Country",fields[i].country);
87     }
88 
89     /*    cJSON_ReplaceItemInObject(cJSON_GetArrayItem(root,1),"City",4)); */
90     out = cJSON_Print(root);
91     cJSON_Delete(root);
92     LOG_I(cjson_example,out);
93     cJSON_free(out);
94     return ret;
95 }

?

二:打印如下

 1 {
 2 
 3     "name":    "Jack ("Bee") Nimble", 4 
 5     "format":    {
 6 
 7         "type":    "rect", 8 
 9         "width":    1920,10 
11         "height":    1080,12 
13         "interlace":    false,14 
15         "frame rate":    24
16 
17     }
18 
19 }
20  
21 ["Sunday","Saturday"]
22  
23 [[0,0],[1,[0,1]]
24  
25 {
26 
27     "Image":    {
28 
29         "Width":    800,30 
31         "Height":    600,32 
33         "Title":    "View from 15th Floor",34 
35         "Thumbnail":    {
36 
37             "Url":    "http:/*www.example.com/image/481989943",38 
39             "Height":    125,40 
41             "Width":    "100"
42 
43         },44 
45         "IDs":    [116,38793]
46 
47     }
48 
49 }
50  
51 [{
52 
53         "precision":    "zip",54 
55         "Latitude":    37.7668,56 
57         "Longitude":    -122.3958999999999,58 
59         "Address":    "",60 
61         "City":    "SAN FRANCISCO",62 
63         "State":    "CA",64 
65         "Zip":    "94107",66 
67         "Country":    "US"
68 
69     },{
70 
71         "precision":    "zip",72 
73         "Latitude":    37.371991,74 
75         "Longitude":    -122.02
76 。。。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读