博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cJSON库解析
阅读量:2066 次
发布时间:2019-04-29

本文共 1548 字,大约阅读时间需要 5 分钟。

最近在使用c语言来编写服务器。json又是比较常用的一种配置说明文件的格式。来记录一下。以资查阅。实在太简单了。

对于cJSON中的节点描述:

/* The cJSON structure: */typedef struct cJSON {    struct cJSON *next,*prev;   /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */    struct cJSON *child;        /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */    int type;                   /* The type of the item, as above. */    char *valuestring;          /* The item's string, if type==cJSON_String */    int valueint;               /* The item's number, if type==cJSON_Number */    double valuedouble;         /* The item's number, if type==cJSON_Number */    char *string;               /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */} cJSON;

将内存解析json

/* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */extern cJSON *cJSON_Parse(const char *value);

如果需要加载的json为

{    "first_blood" : {        "jax" : true    },    "second_blood" : {        "Vayne" : true    }}
cJSON *cjson   = NULL;    cjson = cJSON_Parse(buf);    cJSON *node    = NULL;    cJSON *ele     = NULL;    if (cjson == NULL) {        printf("[IDL] load_idl_file failed, parse file failed, info: %s\n",cfg);        return 1;    }    for ( node = cjson->child; node != NULL; node = node->next) {        fun_name = node->string;        ele      = node->child;        printf("name: %s\n",ele->string);    }

这样也就受用了。

转载地址:http://idfmf.baihongyu.com/

你可能感兴趣的文章
mininet+floodlight搭建sdn环境并创建简答topo
查看>>
【UML】《Theach yourself uml in 24hours》——hour2&hour3
查看>>
【linux】nohup和&的作用
查看>>
【UML】《Theach yourself uml in 24hours》——hour4
查看>>
Set、WeakSet、Map以及WeakMap结构基本知识点
查看>>
【NLP学习笔记】(一)Gensim基本使用方法
查看>>
【NLP学习笔记】(二)gensim使用之Topics and Transformations
查看>>
【深度学习】LSTM的架构及公式
查看>>
【深度学习】GRU的结构图及公式
查看>>
【python】re模块常用方法
查看>>
剑指offer 19.二叉树的镜像
查看>>
剑指offer 20.顺时针打印矩阵
查看>>
剑指offer 21.包含min函数的栈
查看>>
剑指offer 23.从上往下打印二叉树
查看>>
剑指offer 25.二叉树中和为某一值的路径
查看>>
剑指offer 26. 数组中出现次数超过一半的数字
查看>>
剑指offer 27.二叉树的深度
查看>>
剑指offer 29.字符串的排列
查看>>
剑指offer 31.最小的k个树
查看>>
剑指offer 32.整数中1出现的次数
查看>>