c语言json写配置文件,在C语言中解析json配置文件

业务需求

在C或者C++项目中常常需要解析配置文件,我们常见的配置文件格式一般就是.ini,xml,lua或者是一般的text文件,这些格式比较恼人的一个问题就是数据格式过于冗余,或者功能不够强大,不支持正则匹配,或者实现解析文件的代码过多,效率不高等等。比较大型的开源项目,比如Nginx,ATS等都有自己比较庞大的配置文件格式,特别是Nginx,语言十分独特简洁,功能强大,但是往往代码较为繁杂。那么有没有比较简洁的数据交换格式呢?我想到了web上常用到的json格式,这种文件格式非常简洁,而且正在日益成为新的交换格式的标准。为此,我打算在我的项目中使用json作为配置文件。

有没有一种轻量级的,简洁够用的解析实现代码,来完成解析json数据的工作呢?

cJSON简介:

JSON(JavaScriptObject Notation)是一种轻量级的数据交换格式。它基于JavaScript的一个子集。JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯。这些特性使JSON成为理想的数据交换语言。易于人阅读和编写,同时也易于机器解析和生成。

cJSON是一个超轻巧,携带方便,单文件,简单的可以作为ANSI-C标准的JSON解析器。

cJSON结构体:

typedefstruct cJSON {

structcJSON *next,*prev;

struct cJSON *child;

int type;

char * valuestring;

int valueint;

double valuedouble;

char *string;

}cJSON;

1、cJSON存储的时候是采用链表存储的,其访问方式很像一颗树。每一个节点可以有兄妹节点,通过next/prev指针来查找,它类似双向链表;每个节点也可以有孩子节点,通过child指针来访问,进入下一层。

不过,只有节点是对象或数组才可以有孩子节点。

2、type一共有7种取值,分别是:

#define cJSON_False 0

#define cJSON_True 1

#define cJSON_NULL 2

#define cJSON_Number 3

#define cJSON_String 4

#define cJSON_Array 5

#define cJSON_Object 6

若是Number类型,则valueint或valuedouble中存储着值,若你期望的是int,则访问valueint,若期望的是double,则访问valuedouble,可以得到值。

若是String类型的,则valuestring中存储着值,可以访问valuestring得到值。

3、string中存放的是这个节点的名字。

用法:

1、只需在函数中includecJSON.h头文件,然后和cJSON.c或库文件libcJSON.a一起编译即可使用。

2、具体函数用法详见cJSON.h中注释。

4.主要函数说明

extern cJSON *cJSON_Parse(const char *value);//解析一个json字符串为cJSON对象

extern char  *cJSON_Print(cJSON *item);//将json对象转换成容易让人看清结构的字符串

extern char  *cJSON_PrintUnformatted(cJSON *item);//将json对象转换成一个很短的字符串,无回车

extern void   cJSON_Delete(cJSON *c);//删除json对象

extern int  cJSON_GetArraySize(cJSON *array);//返回json数组大小

extern cJSON *cJSON_GetArrayItem(cJSON *array,int item);//返回json数组中指定位置对象

extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);//返回指定字符串对应的json对象

extern cJSON *cJSON_CreateIntArray(int *numbers,int count);//生成整型数组json对象

extern void cJSON_AddItemToArray(cJSON *array, cJSON *item);//向数组中添加元素

5.使用方法(c语言解析json数据)

先看json的数据结构

c中没有对象,所以json数据是采用链表存储的

typedef struct cJSON {

struct cJSON *next,*prev; // 数组 对象数据中用到

struct cJSON *child;  // 数组 和对象中指向子数组对象或值

int type;   // 元素的类型,如是对象还是数组

char *valuestring;   // 如果是字符串

int valueint;    // 如果是数值

double valuedouble;   // 如果类型是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数据

Javascript代码

{

"name": "Jack (\"Bee\") Nimble",

"format": {

"type":       "rect",

"width":      1920,

"height":     1080,

"interlace":  false,

"frame rate": 24

}

}

那么你可以

1:讲字符串解析成json结构体。

cJSON *root = cJSON_Parse(my_json_string);

2:获取某个元素

cJSON *format = cJSON_GetObjectItem(root,"format");

int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint;

3:讲json结构体转换成字符串

char *rendered=cJSON_Print(root);

4:删除

cJSON_Delete(root);

5:构建一个json结构体

cJSON *root,*fmt;

root=cJSON_CreateObject();

cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble"));

cJSON_AddItemToObject(root, "format", fmt=cJSON_CreateObject());

cJSON_AddStringToObject(fmt,"type",     "rect");

cJSON_AddNumberToObject(fmt,"width",        1920);

cJSON_AddNumberToObject(fmt,"height",       1080);

cJSON_AddFalseToObject (fmt,"interlace");

cJSON_AddNumberToObject(fmt,"frame rate",   24);

6.需要澄清的问题

1)cJSON无法区分Object还是Array,它只能通过cJSON中的type字段做区分,这种源码中更能明显看出

2)对外提供的几个接口

/* Get Array size/item / object item. */

int cJSON_GetArraySize(cJSON *array){cJSON *c=array->child;int i=0;while(c)i++,c=c->next;return i;}

cJSON *cJSON_GetArrayItem(cJSON *array,int item){cJSON *c=array->child; while (c && item>0) item--,c=c->next; return c;}

cJSON *cJSON_GetObjectItem(cJSON *object,const char *string){cJSON *c=object->child; while (c && cJSON_strcasecmp(c->string,string)) c=c->next; return c;}

返回的cJSON指针,我们无须再释放;同时

cJSON_GetArraySize返回的是当前子对象数组的大小,如果还有更深的嵌套层次,不考虑;

cJSON_GetArrayItem返回的是下一级子数组中,第item个子数组所在的cJSON对象;

cJSON_GetObjectItem返回的是下一级子对象数组中,名为string的那个子cJSON对象;

如果我们想找第3层的某个名为string的对象,那只能一层一层的遍历,逐层查找,所以有时候不是很方便,具体做法如

cJSON *format = cJSON_GetObjectItem(root,"format");

int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint;

为此,我们写如下函数,去递归查找指定名称的子对象

cJSON* find_Object(cJSON* object, const char* key){

cJSON* subitem = object->child;

while(subitem){

//忽略大小写进行比较

if(!strcasecmp(subitem->string,key))

return subitem;

//有子节点就优先查找子节点

cJSON* tmp = NULL;

if(subitem->child)

tmp = find_Object(subitem,key);

if(tmp) return tmp;

//如果子节点没有找到,返回在本层级查找

subitem = subitem->next;

}

return subitem;

}

3)修改某个子对象的stringvalue值,恐怕不能直接象修改整数的值这样做吧,因为字符串长度会有变化的,而该对象的内存事先已经分配好了

This is an object. We're in C. We don't have objects. But we do have structs.What's the framerate?

cJSON *format = cJSON_GetObjectItem(root,"format");

int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint;

Want to change the framerate?

cJSON_GetObjectItem(format,"frame rate")->valueint=25;

对象的valuestring和string不能修改,只能直接获取,不需要删除,比如

void dump_level_1(cJSON* item){

int i;

for(i=0;i

cJSON* subitem = cJSON_GetArrayItem(item,i);

//这里只能拿到subitem->string,另一个值为空,注意这种方法获取的都是没有双引号的

printf("%s: %s\n",subitem->string,subitem->valuestring);

}

}

另外可参见

http://diaorui.net/archives/245

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值