parse_cfg-cJSON使用-《计算机知识》

admin 2025-11-02 22:35:23 系统网络 来源:ZONE.CI 全球网 0 阅读模式
  • 相关参考
  • 快速使用
    • json格式
    • cjson 使用demo

    目标: 很多程序支持的参数很多,例如图像处理时,牵扯到太多的图像相关细节,可通过json文件快速描述这些参数,软件去解析json文件

    相关参考

    • cjson源码库-github
    • cjson数据格式使用详解

    快速使用

    json格式

    1. /* cJSON Types: */
    2. #define cJSON_False (1 << 0) // 逻辑值 true 或者 false
    3. #define cJSON_True (1 << 1)
    4. #define cJSON_NULL (1 << 2)
    5. #define cJSON_Number (1 << 3) // 数字(整数或浮点数)
    6. #define cJSON_String (1 << 4) // 字符串(双引号中)
    7. #define cJSON_Array (1 << 5) // 数组(在中括号中)
    8. #define cJSON_Object (1 << 6) // 对象(在大括号中)

    例如:

    1. {
    2. "flag":true,
    3. "rouble":null,
    4. "num0":30,
    5. "num1":3.2,
    6. "num2":-3.2,
    7. "ip":"192.168.1.7",
    8. "arr0":[1,2,3],
    9. "arr1":["n1","n2","n3"],
    10. "arr2":[
    11. {"name":"Diaos","age":18},
    12. {"name":"Goddess" ,"age":16}
    13. ]
    14. }

    JSON书写规则1.属性名必须是字符串,且双引号。2.最后一个]或者}后边不能跟”,”,不是最后一个,则后边必须跟“,”3.类型:字符串必须用双引号,可加转义字符”””来表达引号字符串4.不能有注释,可以加入一个不去解析的字段,

    cjson 使用demo

    1. /* The cJSON structure: */
    2. typedef struct cJSON
    3. {
    4. /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
    5. struct cJSON *next;
    6. struct cJSON *prev;
    7. /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
    8. struct cJSON *child; // 子字段
    9. /* The type of the item, as above. */
    10. int type; // 要去解析json的类型,然后用下边的类型去查找
    11. /* The item's string, if type==cJSON_String and type == cJSON_Raw */
    12. char *valuestring;
    13. /* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
    14. int valueint;
    15. /* The item's number, if type==cJSON_Number */
    16. double valuedouble;
    17. /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
    18. char *string; // 当前字段的句柄描述,也就是key
    19. } cJSON;

    通过结构,可以看出:json用一个带子链的链表去描述整体字段,这在内核总线架构中很常见,不在描述。image.png尝试用json去解析上述文件:

    1. #include "cJSON.h"
    2. int parse_config(const char * jsbuf)
    3. {
    4. cJSON *root = NULL;
    5. cJSON *node = NULL;
    6. root = cJSON_Parse(jsbuf);
    7. if ( NULL == root ) {
    8. printf("%s:Parse root info json failed!", __func__);
    9. return -1;
    10. }
    11. node = cJSON_GetObjectItem((cJSON *)root, "flag");
    12. if ( (NULL != node) && (node->type & 3))
    13. printf("flag is %d", node->valueint); // 逻辑值
    14. node = cJSON_GetObjectItem((cJSON *)root, "ip");
    15. if ( NULL != node && (node->type == cJSON_String ))
    16. printf("ip is %s", node->valuestring); // 字符串char*
    17. // 数组
    18. cJSON * node_arr = cJSON_GetObjectItem((cJSON *)root, "arr0");
    19. cJSON* loop;
    20. int index = 0;
    21. cJSON_ArrayForEach(loop, node_arr){
    22. printf("test_arr1[%d] is %d\n",index,loop->valueint);
    23. index ++;
    24. }
    25. cJSON * node_arr = cJSON_GetObjectItem((cJSON *)root, "arr2");
    26. cJSON_ArrayForEach(loop, node_arr){
    27. printf("test_arr2[%d]-name is %s\n",
    28. index,cJSON_GetObjectItem((cJSON *)loop, "name"));
    29. printf("test_arr2[%d]-age is %d\n",
    30. index,cJSON_GetObjectItem((cJSON *)loop, "age"));
    31. index ++;
    32. }
    33. // free一定要放到所有程序最后去释放,否则其中字符串会丢失
    34. cJSON_free(root);
    35. }
    01-shell脚本介绍-《shell脚本》 系统网络

    01-shell脚本介绍-《shell脚本》

    一、shell脚本是什么二、为什么要学shell,而不是其他计算机语言三、学习这门课程的优势四、学了能干什么五、学习什么内容六、学习的技巧七、成长路径八、学习环
    评论:0   参与:  13