zoukankan      html  css  js  c++  java
  • cJSON结构体构建

    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, "%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     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", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
    22  
    23 [[0, -1, 0], [1, 0, 0], [0, 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, 943, 234, 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 。。。
  • 相关阅读:
    学习进度 -- 2019.6.20
    剑指Offer的学习笔记(C#篇)-- 平衡二叉树(二叉树后序遍历递归详解版)
    剑指Offer的学习笔记(C#篇)-- 二叉树的深度(详讲递归)
    剑指Offer的学习笔记(C#篇)-- 数字在排序数组中出现的次数
    if-else判断语句中经常犯的一个错误
    剑指Offer的学习笔记(C#篇)-- 旋转数组的最小数字
    二叉树遍历基础 -- 递归与非递归的实现方法
    剑指Offer的学习笔记(C#篇)-- 序列化二叉树
    剑指Offer的学习笔记(C#篇)-- 对称的二叉树
    剑指Offer的学习笔记(C#篇)-- 二叉树的下一个节点(好理解版本)
  • 原文地址:https://www.cnblogs.com/LiuYanYGZ/p/10259733.html
Copyright © 2011-2022 走看看