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


    1. #ifndef _GP_JSON_H
    2. #define _GP_JSON_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. /// @brief read json text format file, transfer the content to a cJSON object.
    8. /// @param filename json text file path.
    9. /// @return cJSON object
    10. cJSON *json_read_text_file(const char *filename);
    11. /// @brief get a leaf object's string content from a json text file
    12. /// @param filename json text file.
    13. /// @param path from root to leaf, split with "\\", for example "person\\address"
    14. /// @return const char *, no need to free, It storaged in a local buff.
    15. const char *GetJsonStringItem(const char *filename, const char *path);
    16. /// @brief get a leaf object from a json root object
    17. /// @param root Json's root object
    18. /// @param path a string splited with "\\", from root's first childlayer to the remote branch, for example: "provice\\henan\\zhengzhou"
    19. /// @return a cJSON* object, when used yet, should call cJSON_Delete() to release it.
    20. cJSON* getJsonElement(cJSON* root, const char* path);
    21. /// @brief get a leaf object form a json file.
    22. /// @param filename json filename.
    23. /// @param path a string splited with "\\", from root's first childlayer to the remote branch, for example: "provice\\henan\\zhengzhou"
    24. /// @return a cJSON* object, when used yet, should call cJSON_Delete() to release it.
    25. cJSON *GetJsonCommonItem(const char *filename, const char *path);
    26. #endif

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

    1. 接口考虑了相关返回值的析构问题,明确标出的注意事项。
    2. 接口注释使用oxygen语法,现代编译环境(比如VsCode)可以直接在编写代码时,显示函数说明和参数说明。
    3. 实体函数中包含一个未在头文件中给出的测试函数入口。改为main,搭配.json文件可以直接编译测试。
    4. 编译参数也已给出。
    1. /*
    2. * cJson read helper functions.
    3. * first version: Sep13,2023 by fengxh @zz.
    4. * write in linux c99 format.
    5. * main_read_json_text_file() used for test itself.
    6. *
    7. * ver0.1.20230914 by fengxh @zz.
    8. * + created and tested yet.
    9. * helped by ChatGPT3.5.
    10. * build cmd line : gcc gpjson.c -lcjson
    11. */
    12. #include
    13. #include
    14. #include
    15. #include
    16. #include "gpjson.h"
    17. cJSON *json_read_text_file(const char *filename){
    18. FILE *file = fopen(filename, "r");
    19. if (file == NULL) {
    20. printf("Failed to open the file.\n");
    21. return NULL;
    22. }
    23. fseek(file, 0, SEEK_END);
    24. long file_size = ftell(file);
    25. fseek(file, 0, SEEK_SET);
    26. char *file_content = (char *)malloc(file_size + 1);
    27. fread(file_content, 1, file_size, file);
    28. file_content[file_size] = '\0';
    29. fclose(file);
    30. cJSON *json = cJSON_Parse(file_content);
    31. free(file_content);
    32. if (json == NULL) {
    33. printf("Failed to parse JSON.\n");
    34. free(file_content);
    35. return NULL;
    36. }
    37. return json;
    38. }
    39. cJSON* getJsonElement(cJSON* root, const char* path) {
    40. cJSON* current = root;
    41. char* path_copy = strdup(path); // 复制路径,以便分割
    42. char* token = strtok(path_copy, "\\"); // 使用 "\" 分割路径
    43. while (token != NULL) {
    44. if (current != NULL && cJSON_IsObject(current)) {
    45. current = cJSON_GetObjectItem(current, token);
    46. } else {
    47. current = NULL; // 节点不存在
    48. break;
    49. }
    50. token = strtok(NULL, "\\");
    51. }
    52. free(path_copy);
    53. return current;
    54. }
    55. cJSON *GetJsonCommonItem(const char *filename, const char *path)
    56. {
    57. cJSON *root = json_read_text_file(filename);
    58. if(root == NULL) return NULL;
    59. cJSON *ret = getJsonElement(root, path);
    60. if(ret != NULL)
    61. {
    62. cJSON_Delete(root);
    63. return cJSON_Duplicate(ret, 1);
    64. }
    65. cJSON_Delete(root);
    66. return NULL;
    67. }
    68. const char *GetJsonStringItem(const char *filename, const char *path)
    69. {
    70. static char cache[1024*20];
    71. cJSON *root = json_read_text_file(filename);
    72. if(root == NULL) return NULL;
    73. // 通过路径获取最终的cJSON元素
    74. cJSON* element = getJsonElement(root, path);
    75. if (element != NULL && element->type == cJSON_String) {
    76. strncpy(cache, element->valuestring, 1024*20-1);
    77. } else {
    78. return NULL;
    79. }
    80. // 释放cJSON对象内存
    81. cJSON_Delete(root);
    82. return cache;
    83. }
    84. // 测试程序,可以查看效果。
    85. int main_read_json_text_file(void){
    86. cJSON *json = json_read_text_file("data.json");
    87. printf("%s\n", cJSON_Print(json));
    88. cJSON_Delete(json);
    89. return 0;
    90. }

  • 相关阅读:
    兼顾省钱与性能的存储资源盘活系统
    【AHK】任务栏调节音量/边缘滚动调节/边缘触发
    Note——time
    百度文心一率先言向全社会开放 应用商店搜“文心一言”可直接下载
    常用类和内部类总结
    axios返回几种数据格式? 其中Blob返回时的size是什么意思?
    Linux--CentOS6和CentOS7的区别
    python爬虫
    【Java分享客栈】我曾经的两个Java老师一个找不到工作了一个被迫转行了
    H3C IPsec多分支经由总部互通
  • 原文地址:https://blog.csdn.net/twicave/article/details/132868480