C語言 | 解析json

// 用cjson.c和cjson.h讀取json文件,保存json文件

#include "cJson.h"

/*
// 示例json,名稱爲1.json
[
    {
        "ImgName":"abc.jpg"
        "ImgInfo":
        {
            "ImgSize":
            [
                1920,
                1080
            ]
        },
        "person":
        [
            {
            "rect":
            [
                [
                    25,
                    30
                ],
                [
                    150,
                    200
                ]
            ],
            "id":1
        },
        {
            "rect":
            [
                [
                    25,
                    30
                ],
                [
                    151,
                    201
                ]
            ],
            "id":2
        },
        {
            "rect":
            [
                [
                    25,
                    30
                ],
                [
                    152,
                    202
                ]
            ],
            "id":3
        }

        ]
        
    }
]
*/

cJSON* Parse_json(std::string picJsonName)
{
    char *content;                             // 文件內容
    long  len;                                 // 文件長度
    FILE *fp_json = NULL;                      // 文件指針
    cJSON *json_input = NULL;

    fp_json = fopen(picJsonName.c_str(),"rb"); // c_str: 將string轉換爲char*
    fseek(fp_json,0,SEEK_END);                 // 文件指針指向尾部
    len = ftell(fp_json);                      // ftell:用於得到文件位置指針當前位置相對於文件首的偏移字節數
    fseek(fp_json,0,SEEK_SET);                 // 文件指針指向頭部
    content = (char *)malloc(len+1);           //爲json格式的文件分配內存
    fread(content,1,len,fp_json);   
    fclose(fp_json);

    json_input = cJSON_Pare(content);          // 解析json接口
    free(content);

    return json_input;

}


int main()
{
    std::string picJsonName = "./data/1.json"
    cJSON *json_input = NULL;
    cJSON *json_input_temp = NULL;
    int input_img_width = 0;
    int input_img_height = 0;
    int person_num = 0;
    int left_up_x = 0;
    int left_up_y = 0;
    int right_down_x = 0;
    int right_down_y = 0;

    json_input = Parse_json(picJsonName);

    // 獲取圖像寬高
    json_input_temp = cJSON_GetArrayItem((cJSON_GetObjectItem(cJSON_GetObjectItem(json_input,"ImgInfo"),"ImgSize"),0);
    input_img_width = json_input_temp->valueint;
    json_input_temp = cJSON_GetArrayItem((cJSON_GetObjectItem(cJSON_GetObjectItem(json_input,"ImgInfo"),"ImgSize"),1);
    input_img_height = json_input_temp->valueint;
    // 獲取json中person的個數
    person_num = cJSON_GetArraySize(cJSON_GetObjectItem(json_input,"person"));
    // 獲取json中每個person的四個座標值
    for (int i =0; i < person_num; i++)
    {
        left_up_x = cJSON_GetArrayItem(cJSON_GetArrayItem(cJSON_GetObjectItem(cJSON_GetArrayItem(cJSON_GetObjectItem(json_input,"person"),i),"rect"),0),0);
        left_up_y = cJSON_GetArrayItem(cJSON_GetArrayItem(cJSON_GetObjectItem(cJSON_GetArrayItem(cJSON_GetObjectItem(json_input,"person"),i),"rect"),0),1);
        right_down_x = cJSON_GetArrayItem(cJSON_GetArrayItem(cJSON_GetObjectItem(cJSON_GetArrayItem(cJSON_GetObjectItem(json_input,"person"),i),"rect"),1),0);
        right_down_y = cJSON_GetArrayItem(cJSON_GetArrayItem(cJSON_GetObjectItem(cJSON_GetArrayItem(cJSON_GetObjectItem(json_input,"person"),i),"rect"),1),1);
        printf("%d, %d, %d, %d \n",left_up_x,left_up_y,right_down_x,right_down_y);
    }



    return 1;


}












發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章