mjson学习的简单例子分享

01
#include
02
#include
03
#include
04
int main()
05
{
06
    json_t *entry, *root, *head, *body, *label, *value;
07
    char *document;
08
    root = json_new_object();
09
     
10
    /*--------Head------*/
11

12
    head = json_new_string("head");
13
    entry = json_new_object();
14
     
15
    label = json_new_string("ID");
16
    value = json_new_number("1");
17
    json_insert_child(label,value);
18
    json_insert_child(entry,label);
19
     
20
    label = json_new_string("Tyep");
21
    value = json_new_string("0");
22
    json_insert_child(label,value);
23
    json_insert_child(entry,label);
24
     
25
     
26
    json_insert_child(head, entry);
27
    /*------End Head------*/
28

29
    /*---------Body---------*/
30
    body = json_new_string("body");
31
    entry = json_new_object();
32
     
33
    label = json_new_string("phone");
34
    value = json_new_string("028-2306987");
35
    json_insert_child(label,value);
36
    json_insert_child(entry, label);
37

38
    label = json_new_string("address");
39
    value = json_new_string("Los Angeles");
40
    json_insert_child(label,value);
41
    json_insert_child(entry,label);
42

43
    label = json_new_string("name");
44
    json_t *array = json_new_array();
45
    value = json_new_string("Jack");
46
    json_insert_child(array,value);
47
    value = json_new_string("Mike");
48
    json_insert_child(array,value);
49
    value = json_new_string("Kobe");
50
    json_insert_child(array,value);
51
    json_insert_child(label,array);
52
    json_insert_child(entry,label);
53
     
54
    json_insert_child(body,entry);
55
    /*------End Body-------*/
56

57
    json_insert_child(root,head);
58
    json_insert_child(root,body);
59

60
    json_tree_to_string(root,&document);
61
    printf("%s\n", document);
62
    json_free_value(&root);
63

64

65
    /*---Parsing the documents---*/
66
    json_parse_document(&root,document);
67
     
68
    head = json_find_first_label(root,"head");
69
    body = json_find_first_label(root,"body");
70
     
71
    label = json_find_first_label(head->child,"ID");
72
    printf("%s\n",label->child->text);
73

74

75
    /*clean up*/
76
    free(document);
77
    json_free_value(&root);
78
    exit(0);
简洁tab标签
文章来源: http://www.huiyi8.com/tab/jianjie/

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29691626/viewspace-1188466/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29691626/viewspace-1188466/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值