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

  • 相关阅读:
    flask基础 MUI
    flask基础 MongoDB
    falsk 基础 语音识别与语音合成()
    flask基础 websocket ()
    flask基础四 请求上下文()
    flask基础三
    学习整理
    Elasticsearch
    课程学习:Linux系统管理
    课程学习:程序设计与算法
  • 原文地址:https://www.cnblogs.com/Toya/p/13864479.html
Copyright © 2011-2022 走看看