cjson构建_cJSON结构体构建

1 intcreate_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, "%s\n", 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, "%s\n", out);34 cJSON_free(out);35

36 //------------------构建第3个----------------------

37 /*The matrix:*/

38 int numbers[3][3] = {{0, -1, 0}, {1, 0, 0}, {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, "%s\n", 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, "Height", 125);61 cJSON_AddStringToObject(thm, "Width", "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, "%s\n", 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", "CA", "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",cJSON_CreateIntArray(ids,4));*/

90 out =cJSON_Print(root);91 cJSON_Delete(root);92 LOG_I(cjson_example, "%s\n", out);93 cJSON_free(out);94 returnret;95 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值