cJSON库解析

最近在使用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);
    }

这样也就受用了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值