0
2
| MJSON是使用ISOC标准库开发的json解析库。
下载地址:
http://www.linuxpig.com/softwares/mjson/mjson-0.9.tar.bz2
安装:
解压出来,然后直接包含json.h就可以了。
下面是几个官方给出的几个例子,由于直接拷贝过来编译没通过,做了一些修改,详见注释部分。
【mjson例一】
viewsource
print?
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(); |
19 | //{"entry":{"name":"Andew","phone":"555123456"}}
|
21 | //insertthefirstlabel-valuepair
|
22 | label=json_new_string( "name" ); |
23 | value=json_new_string( "Andew" ); |
24 | json_insert_child(label,value); |
25 | json_insert_child(entry,label); |
27 | //insertthesecondlabel-valuepair
|
28 | label=json_new_string( "phone" ); |
29 | value=json_new_string( "555123456" ); |
30 | json_insert_child(label,value); |
31 | json_insert_child(entry,label); |
33 | //insertsthatobjectasavalueinalabel-valuepair
|
34 | label=json_new_string( "entry" ); |
35 | json_insert_child(label,entry); |
37 | //insertsthatlabel-valuepairintotherootobject
|
38 | json_insert_child(root,label); |
41 | json_tree_to_string(root,&text); |
46 | json_free_value(&root); |
50 | //["test1","test2",109]
|
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例二】
viewsource
print?
07 | json_t*new_entry( char
*name, char *phone)
|
09 | json_t*entry,*label,*value; |
12 | entry=json_new_object(); |
14 | //insertthefirstlabel-valuepair
|
15 | label=json_new_string( "name" ); |
16 | value=json_new_string( "Andew" ); |
17 | json_insert_child(label,value); |
18 | json_insert_child(entry,label); |
20 | //insertthesecondlabel-valuepair
|
21 | label=json_new_string( "phone" ); |
22 | value=json_new_string( "555123456" ); |
23 | json_insert_child(label,value); |
24 | json_insert_child(entry,label); |
26 | //insertsthatobjectasavalueinalabel-valuepair
|
27 | label=json_new_string( "entry" ); |
28 | json_insert_child(label,entry); |
35 | setlocale
(LC_ALL, "" ); //设置为系统默认的地域信息 |
40 | root=json_new_object(); |
42 | //createsthedesiredMJSONdocumentsubtree
|
43 | subtree=new_entry( "Andrew" , "555123456" );
|
45 | //insertsthesubtreeintotherootobject
|
46 | json_insert_child(root,subtree); |
50 | json_tree_to_string(root,&text); |
51 | printf ( "%s\n" ,text); //官方例子中为printf("%ls\n",text);去掉l才能打印出来。。
|
55 | json_free_value(&root); |
60 | { "entry" :{ "name" : "Andew" , "phone" : "555 123456" }} | 【mjson例三】
viewsource
print?
10 | char
*document= "{\"entry\":{\"name\":\"Andew\",\"phone\":\"555123456\"}}" ; |
14 | printf ( "Parsingthedocument…\n" ); |
15 | root=json_parse_document(document); |
17 | printf ( "Printingthedocumenttree…\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/
|