加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cJSON库解析

发布时间:2020-12-16 18:55:45 所属栏目:百科 来源:网络整理
导读:最近在使用c语言来编写服务器。json又是比较常用的一种配置说明文件的格式。来记录一下。以资查阅。实在太简单了。 (下载地址)[https://sourceforge.net/projects/cjson/] 对于cJSON中的节点描述: /* The cJSON structure: */typedef struct cJSON { struct

最近在使用c语言来编写服务器。json又是比较常用的一种配置说明文件的格式。来记录一下。以资查阅。实在太简单了。
(下载地址)[https://sourceforge.net/projects/cjson/]

对于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: %sn",cfg);
        return 1;
    }
    for ( node = cjson->child; node != NULL; node = node->next) {
        fun_name = node->string;
        ele      = node->child;
        printf("name: %sn",ele->string);
    }

这样也就受用了。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读