zoukankan      html  css  js  c++  java
  • cJSON

    转载:https://github.com/DaveGamble/cJSON

    cJSON的使用:

     把这两个文件放到自己的工程

    实例:

    建立和解析这个json格式的字符串

    {
        "name": "Awesome 4K",
        "resolutions": [
            {
                "width": 1280,
                "height": 720
            },
            {
                "width": 1920,
                "height": 1080
            },
            {
                "width": 3840,
                "height": 2160
            }
        ]
    }
      1 #include<iostream>
      2 #include"cJSON.h"
      3 
      4 using namespace std;
      5 
      6 //create a monitor with a list of supported resolutions
      7 //NOTE: Returns a heap allocated string, you are required to free it after use.
      8 char *create_monitor(void)
      9 {
     10     const unsigned int resolution_numbers[3][2] = {
     11         {1280, 720},
     12         {1920, 1080},
     13         {3840, 2160}
     14     };
     15     char *string = NULL;
     16     cJSON *name = NULL;
     17     cJSON *resolutions = NULL;
     18     cJSON *resolution = NULL;
     19     cJSON *width = NULL;
     20     cJSON *height = NULL;
     21     size_t index = 0;
     22 
     23     cJSON *monitor = cJSON_CreateObject();
     24     if (monitor == NULL)
     25     {
     26         goto end;
     27     }
     28 
     29     name = cJSON_CreateString("Awesome 4K");
     30     if (name == NULL)
     31     {
     32         goto end;
     33     }
     34     /* after creation was successful, immediately add it to the monitor,
     35      * thereby transferring ownership of the pointer to it */
     36     cJSON_AddItemToObject(monitor, "name", name);
     37 
     38     resolutions = cJSON_CreateArray();
     39     if (resolutions == NULL)
     40     {
     41         goto end;
     42     }
     43     cJSON_AddItemToObject(monitor, "resolutions", resolutions);
     44 
     45     for (index = 0; index < (sizeof(resolution_numbers) / (2 * sizeof(int))); ++index)
     46     {
     47         resolution = cJSON_CreateObject();
     48         if (resolution == NULL)
     49         {
     50             goto end;
     51         }
     52         cJSON_AddItemToArray(resolutions, resolution);
     53 
     54         width = cJSON_CreateNumber(resolution_numbers[index][0]);
     55         if (width == NULL)
     56         {
     57             goto end;
     58         }
     59         cJSON_AddItemToObject(resolution, "width", width);
     60 
     61         height = cJSON_CreateNumber(resolution_numbers[index][1]);
     62         if (height == NULL)
     63         {
     64             goto end;
     65         }
     66         cJSON_AddItemToObject(resolution, "height", height);
     67     }
     68 
     69     string = cJSON_Print(monitor);
     70     if (string == NULL)
     71     {
     72         fprintf(stderr, "Failed to print monitor.
    ");
     73     }
     74 
     75 end:
     76     cJSON_Delete(monitor);
     77     return string;
     78 }
     79 
     80 /* return 1 if the monitor supports full hd, 0 otherwise */
     81 int supports_full_hd(const char * const monitor)
     82 {
     83     const cJSON *resolution = NULL;
     84     const cJSON *resolutions = NULL;
     85     const cJSON *name = NULL;
     86     int status = 0;
     87     cJSON *monitor_json = cJSON_Parse(monitor);
     88     if (monitor_json == NULL)
     89     {
     90         const char *error_ptr = cJSON_GetErrorPtr();
     91         if (error_ptr != NULL)
     92         {
     93             fprintf(stderr, "Error before: %s
    ", error_ptr);
     94         }
     95         status = 0;
     96         goto end;
     97     }
     98 
     99     name = cJSON_GetObjectItemCaseSensitive(monitor_json, "name");
    100     if (cJSON_IsString(name) && (name->valuestring != NULL))
    101     {
    102         printf("Checking monitor "%s"
    ", name->valuestring);
    103     }
    104 
    105     resolutions = cJSON_GetObjectItemCaseSensitive(monitor_json, "resolutions");
    106     cJSON_ArrayForEach(resolution, resolutions)
    107     {
    108         cJSON *width = cJSON_GetObjectItemCaseSensitive(resolution, "width");
    109         cJSON *height = cJSON_GetObjectItemCaseSensitive(resolution, "height");
    110 
    111         if (!cJSON_IsNumber(width) || !cJSON_IsNumber(height))
    112         {
    113             status = 0;
    114             goto end;
    115         }
    116 
    117         int w = width->valueint;
    118         int h = height->valueint;
    119         cout << "(w h) (" << w << " " << h << ")
    ";
    120 
    121         if ((width->valuedouble == 1920) && (height->valuedouble == 1080))
    122         {
    123             status = 1;
    124             goto end;
    125         }
    126     }
    127 
    128 end:
    129     cJSON_Delete(monitor_json);
    130     return status;
    131 }
    132 
    133 int main()
    134 {
    135     char *strJson = create_monitor();
    136     cout << strJson << "
    ";
    137     supports_full_hd(strJson);
    138     free(strJson);
    139     system("pause");
    140     return 0;
    141 }

    另一个json库:jsoncpp github地址:https://github.com/open-source-parsers/jsoncpp

  • 相关阅读:
    保持唯一性,请停止使用【python3 内置hash() 函数】
    彻底解决go get golang.org/x等包失败与VSCode golang插件安装失败问题
    Linux 任务后台运行软件【即:终端复用器】之---screen
    Ubuntu+uWSGI部署基于Django的API【鸿篇巨制,事无巨细】
    python慎用os.getcwd() ,除非你知道【文件路径与当前工作路径的区别】
    win下youtube-dl 【ERROR: requested format not available】选下载视频质量的坑--【值得一看】
    Mysql失败,异常pymysql.err.InternalError: (1366, "Incorrect string value: '\xF0\x9D\x90\xBF;......
    scrapy post payload的坑及相关知识的补充【POST传参方式的说明及scrapy和requests实现】
    mitmproxy--Cannot establish TLS with client (sni: e.crashlytics.com): TlsException("(-1, 'Unexpected EOF')",) 解决办法
    【GET TIPS】Chrome所见即所得的截图技巧
  • 原文地址:https://www.cnblogs.com/Toya/p/13864479.html
Copyright © 2011-2022 走看看