天天看点

cjson学习篇五:gitHub示例

说明:

  本文章旨在总结备份、方便以后查询,由于是个人总结,如有不对,欢迎指正;另外,内容大部分来自网络、书籍、和各类手册,如若侵权请告知,马上删帖致歉。

  QQ 群 号:513683159 【相互学习】

内容来源:

示例一:构建下面JSON

{
    "name": "Awesome 4K",
    "resolutions": [
        {
            "width": 1280,
            "height": 720
        },
        {
            "width": 1920,
            "height": 1080
        },
        {
            "width": 3840,
            "height": 2160
        }
    ]
}
           

方法一:(去掉健壮性判断)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"
//create a monitor with a list of supported resolutions
//NOTE: Returns a heap allocated string, you are required to free it after use.
char *create_monitor(void)
{
    const unsigned int resolution_numbers[3][2] = {
        {1280, 720},
        {1920, 1080},
        {3840, 2160}
    };
    char *string = NULL;
    cJSON *name = NULL;
    cJSON *resolutions = NULL;
    cJSON *resolution = NULL;
    cJSON *width = NULL;
    cJSON *height = NULL;
    size_t index = 0;

    cJSON *monitor = cJSON_CreateObject();

    name = cJSON_CreateString("Awesome 4K");
    cJSON_AddItemToObject(monitor, "name", name);

    resolutions = cJSON_CreateArray();
    cJSON_AddItemToObject(monitor, "resolutions", resolutions);

    for (index = 0; index < (sizeof(resolution_numbers) / (2 * sizeof(int))); ++index)
    {
        resolution = cJSON_CreateObject();
        cJSON_AddItemToArray(resolutions, resolution);

        width = cJSON_CreateNumber(resolution_numbers[index][0]);
        cJSON_AddItemToObject(resolution, "width", width);

        height = cJSON_CreateNumber(resolution_numbers[index][1]);
        cJSON_AddItemToObject(resolution, "height", height);
    }

    string = cJSON_Print(monitor);
}

int main(void)
{
    char * string  = create_monitor();
    printf("%s\n",string);    
    return 0;
}
           

方法二:(使用

cJSON_Add… ToObject

变得更简单些)(去掉健壮性判断)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"

char *create_monitor_with_helpers(void)
{
    const unsigned int resolution_numbers[3][2] = {
        {1280, 720},
        {1920, 1080},
        {3840, 2160}
    };
    char *string = NULL;
    cJSON *resolutions = NULL;
    size_t index = 0;
    cJSON *monitor = cJSON_CreateObject();
    cJSON_AddStringToObject(monitor, "name", "Awesome 4K") ;
    resolutions = cJSON_AddArrayToObject(monitor, "resolutions");

    for (index = 0; index < (sizeof(resolution_numbers) / (2 * sizeof(int))); ++index)
    {
        cJSON *resolution = cJSON_CreateObject();

       cJSON_AddNumberToObject(resolution, "width", resolution_numbers[index][0]);

       cJSON_AddNumberToObject(resolution, "height", resolution_numbers[index][1]);

       cJSON_AddItemToArray(resolutions, resolution);
    }
    string = cJSON_Print(monitor);
}

int main(void)
{
    char * string  = create_monitor_with_helpers();
    printf("%s\n",string);    
    return 0;
}
           

示例二:解析上面JSON,并检查监视器是否支持全高清分辨率,同时打印一些诊断输出: 。

不清楚这个是怎么用的

/* return 1 if the monitor supports full hd, 0 otherwise */
int supports_full_hd(const char * const monitor)
{
    const cJSON *resolution = NULL;
    const cJSON *resolutions = NULL;
    const cJSON *name = NULL;
    int status = 0;
    cJSON *monitor_json = cJSON_Parse(monitor);
    if (monitor_json == NULL)
    {
        const char *error_ptr = cJSON_GetErrorPtr();
        if (error_ptr != NULL)
        {
            fprintf(stderr, "Error before: %s\n", error_ptr);
        }
        status = 0;
        goto end;
    }

    name = cJSON_GetObjectItemCaseSensitive(monitor_json, "name");
    if (cJSON_IsString(name) && (name->valuestring != NULL))
    {
        printf("Checking monitor \"%s\"\n", name->valuestring);
    }

    resolutions = cJSON_GetObjectItemCaseSensitive(monitor_json, "resolutions");
    cJSON_ArrayForEach(resolution, resolutions)
    {
        cJSON *width = cJSON_GetObjectItemCaseSensitive(resolution, "width");
        cJSON *height = cJSON_GetObjectItemCaseSensitive(resolution, "height");

        if (!cJSON_IsNumber(width) || !cJSON_IsNumber(height))
        {
            status = 0;
            goto end;
        }

        if ((width->valuedouble == 1920) && (height->valuedouble == 1080))
        {
            status = 1;
            goto end;
        }
    }

end:
    cJSON_Delete(monitor_json);
    return status;
}