一组完整的读Json配置信息的辅助函数

文章介绍了用于处理JSON文件的cJSON库中的四个辅助函数,包括读取文本文件内容、根据路径获取叶子节点对象和字符串、以及测试程序。这些函数允许通过指定文件名和路径结构访问JSON数据。
摘要由CSDN通过智能技术生成
#ifndef _GP_JSON_H
#define _GP_JSON_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cjson/cJSON.h>

/// @brief read json text format file, transfer the content to a cJSON object.
/// @param filename json text file path.
/// @return cJSON object
cJSON *json_read_text_file(const char *filename);

/// @brief get a leaf object's string content from a json text file
/// @param filename json text file.
/// @param path from root to leaf, split with "\\", for example "person\\address"
/// @return const char *, no need to free, It storaged in a local buff.
const char *GetJsonStringItem(const char *filename, const char *path);

/// @brief get a leaf object from a json root object
/// @param root Json's root object
/// @param path a string splited with "\\", from root's first childlayer to the remote branch, for example: "provice\\henan\\zhengzhou"
/// @return a cJSON* object, when used yet, should call cJSON_Delete() to release it.
cJSON* getJsonElement(cJSON* root, const char* path);

/// @brief get a leaf object form a json file.
/// @param filename json filename.
/// @param path a string splited with "\\", from root's first childlayer to the remote branch, for example: "provice\\henan\\zhengzhou"
/// @return a cJSON* object, when used yet, should call cJSON_Delete() to release it.
cJSON *GetJsonCommonItem(const char *filename, const char *path);

#endif

上面是用来读取json对象,或者json文件的一组辅助函数接口。它可以仅仅通过指定json文件名,和json层级结构相关的路径信息,来返回json文件中的某一个叶子节点;甚至可以直接返回字符串信息。

  1. 接口考虑了相关返回值的析构问题,明确标出的注意事项。
  2. 接口注释使用oxygen语法,现代编译环境(比如VsCode)可以直接在编写代码时,显示函数说明和参数说明。
  3. 实体函数中包含一个未在头文件中给出的测试函数入口。改为main,搭配.json文件可以直接编译测试。
  4. 编译参数也已给出。
/*
 * cJson read helper functions.
 * first version: Sep13,2023 by fengxh @zz.
 * write in linux c99 format. 
 * main_read_json_text_file() used for test itself.
 * 
 * ver0.1.20230914 by fengxh @zz.
 *    + created and tested yet.
 *      helped by ChatGPT3.5.
 *      build cmd line : gcc gpjson.c -lcjson
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cjson/cJSON.h>
#include "gpjson.h"

cJSON *json_read_text_file(const char *filename){
    FILE *file = fopen(filename, "r");
    if (file == NULL) {
        printf("Failed to open the file.\n");
        return NULL;
    }

    fseek(file, 0, SEEK_END);
    long file_size = ftell(file);
    fseek(file, 0, SEEK_SET);

    char *file_content = (char *)malloc(file_size + 1);
    fread(file_content, 1, file_size, file);
    file_content[file_size] = '\0';

    fclose(file);

    cJSON *json = cJSON_Parse(file_content);
    free(file_content);
    if (json == NULL) {
        printf("Failed to parse JSON.\n");
        free(file_content);
        return NULL;
    }


    return json;
}


cJSON* getJsonElement(cJSON* root, const char* path) {
    cJSON* current = root;
    char* path_copy = strdup(path);  // 复制路径,以便分割
    char* token = strtok(path_copy, "\\");  // 使用 "\" 分割路径

    while (token != NULL) {
        if (current != NULL && cJSON_IsObject(current)) {
            current = cJSON_GetObjectItem(current, token);
        } else {
            current = NULL;  // 节点不存在
            break;
        }

        token = strtok(NULL, "\\");
    }

    free(path_copy);
    return current;
}

cJSON *GetJsonCommonItem(const char *filename, const char *path)
{
     cJSON *root = json_read_text_file(filename);
     if(root == NULL) return NULL;
     cJSON *ret = getJsonElement(root, path);
     if(ret != NULL)
     {
        cJSON_Delete(root);
        return cJSON_Duplicate(ret, 1);
     }
    cJSON_Delete(root);
    return NULL;
}


const char *GetJsonStringItem(const char *filename, const char *path)
{
    static char cache[1024*20];

    cJSON *root = json_read_text_file(filename);
    if(root == NULL) return NULL;

    // 通过路径获取最终的cJSON元素
    cJSON* element = getJsonElement(root, path);
    if (element != NULL && element->type == cJSON_String) {
        strncpy(cache, element->valuestring, 1024*20-1);
    } else {
        return NULL;
    }
    // 释放cJSON对象内存
    cJSON_Delete(root);
    return cache;
}


// 测试程序,可以查看效果。
int main_read_json_text_file(void){

	cJSON *json = json_read_text_file("data.json");
    printf("%s\n", cJSON_Print(json));
    cJSON_Delete(json);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子正

thanks, bro...

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值