下载地址:
http://www.linuxpig.com/softwares/mjson/mjson-0.9.tar.bz2
安装:
解压出来,然后直接包含 json.h 就可以了。
下面是几个官方给出的几个例子,由于直接拷贝过来编译没通过,做了一些修改,详见注释部分。
【mjson例一】
01 | <strong>#include <stdio.h> |
10 | json_t *root, *entry, *label, *value; |
11 | setlocale (LC_ALL, "" ); |
14 | root = json_new_object(); |
16 | entry = json_new_object(); |
22 | label = json_new_string( "name" ); |
23 | value = json_new_string( "Andew" ); |
24 | json_insert_child(label, value); |
25 | json_insert_child(entry, label); |
28 | label = json_new_string( "phone" ); |
29 | value = json_new_string( "555 123 456" ); |
30 | json_insert_child(label, value); |
31 | json_insert_child(entry, label); |
34 | label = json_new_string( "entry" ); |
35 | json_insert_child(label, entry); |
38 | json_insert_child(root, label); |
41 | json_tree_to_string(root, &text); |
46 | json_free_value(&root); |
52 | root = json_new_array(); |
53 | label = json_new_string( "test1" ); |
54 | json_insert_child(root,label); |
55 | value = json_new_string( "test2" ); |
56 | json_insert_child(root,value); |
57 | value = json_new_number( "109" ); |
58 | json_insert_child(root,value); |
60 | json_tree_to_string(root,&text); |
65 | json_free_value(&root); |
【mjson例二】
07 | json_t *new_entry( char *name, char *phone) |
09 | json_t *entry, *label, *value; |
12 | entry = json_new_object(); |
15 | label = json_new_string( "name" ); |
16 | value = json_new_string( "Andew" ); |
17 | json_insert_child(label, value); |
18 | json_insert_child(entry, label); |
21 | label = json_new_string( "phone" ); |
22 | value = json_new_string( "555 123 456" ); |
23 | json_insert_child(label, value); |
24 | json_insert_child(entry, label); |
27 | label = json_new_string( "entry" ); |
28 | json_insert_child(label, entry); |
35 | setlocale (LC_ALL, "" ); |
37 | json_t *root, *subtree; |
40 | root = json_new_object(); |
43 | subtree = new_entry( "Andrew" , "555 123 456" ); |
46 | json_insert_child(root, subtree); |
50 | json_tree_to_string(root, &text); |
55 | json_free_value(&root); |
60 | { "entry" :{ "name" : "Andew" , "phone" : "555 123 456" }} |
【mjson例三】
09 | setlocale (LC_ALL, "" ); |
10 | char *document = "{\"entry\":{\"name\":\"Andew\",\"phone\":\"555 123 456\"}}" ; |
14 | printf ( "Parsing the document…\n" ); |
15 | root = json_parse_document(document); |
17 | printf ( "Printing the document tree…\n" ); |
18 | json_tree_to_string(root, &document); |
19 | wprintf( "%ls\n" , document); |
22 | json_free_value(&root); |
【参考】
http://www.json.org/json-zh.html
http://mjson.sourceforge.net/
我使用的是cJSONcJSONFiles
先看json的数据结构
c中没有对象,所以json数据是采用链表存储的
01.
typedef struct cJSON {
02.
struct cJSON *next,*prev; // 数组 对象数据中用到
03.
struct cJSON *child; // 数组 和对象中指向子数组对象或值
04.
05.
int type; // 元素的类型,如是对象还是数组
06.
07.
char *valuestring; // 如果是字符串
08.
int valueint; // 如果是数值
09.
double
valuedouble; // 如果类型是cJSON_Number
10.
11.
char *string; // The item's name string, if this item is the child of, or is in the list of subitems of an object.
12.
} cJSON;
比如你有一个json数据
01.
{
02.
"name"
:
"Jack (\"Bee\") Nimble"
,
03.
"format"
: {
04.
"type"
:
"rect"
,
05.
"width"
:
1920
,
06.
"height"
:
1080
,
07.
"interlace"
: false,
08.
"frame rate"
:
24
09.
}
10.
}
那么你可以
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结构体
01.
cJSON *root,*fmt;
02.
root=cJSON_CreateObject();
03.
cJSON_AddItemToObject(root,
"name"
, cJSON_CreateString(
"Jack (\"Bee\") Nimble"
));
04.
cJSON_AddItemToObject(root,
"format"
, fmt=cJSON_CreateObject());
05.
cJSON_AddStringToObject(fmt,
"type"
,
"rect"
);
06.
cJSON_AddNumberToObject(fmt,
"width"
,
1920
);
07.
cJSON_AddNumberToObject(fmt,
"height"
,
1080
);
08.
cJSON_AddFalseToObject (fmt,
"interlace"
);
09.
cJSON_AddNumberToObject(fmt,
"frame rate"
,
24
);