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", 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", 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", 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", 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", out);93 cJSON_free(out);94 returnret;95 }

GitHub 加速计划 / cj / cJSON
10.45 K
3.16 K
下载
Ultralightweight JSON parser in ANSI C
最近提交(Master分支:2 个月前 )
424ce4ce This reverts commit 5b502cdbfb21fbe5f6cf9ffbd2b96e4281a741e6. Related to #860 4 个月前
32497300 - 5 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐