C语言 JSON 解析库 - MJSON使用介绍

0
2

MJSON 是使用 ISO C 标准库开发的 json 解析库。

 

下载地址:
http://www.linuxpig.com/softwares/mjson/mjson-0.9.tar.bz2

 

安装:
解压出来,然后直接包含 json.h 就可以了。

下面是几个官方给出的几个例子,由于直接拷贝过来编译没通过,做了一些修改,详见注释部分。

【mjson例一】

01<STRONG>#include <stdio.h>
02#include <stdlib.h>
03#include <locale.h>
04  
05#include "json.h"
06  
07int main (void)
08{
09    char *text;
10    json_t *root, *entry, *label, *value;
11    setlocale (LC_ALL, "");//设为系统默认地域信息
12  
13    // creates the root node
14    root = json_new_object();
15    // create an entry node
16    entry = json_new_object();
17  
18//    第一部分,打印结果:
19//    {"entry":{"name":"Andew","phone":"555 123 456"}}
20  
21    // insert the first label-value pair
22    label = json_new_string("name");
23    value = json_new_string("Andew");
24    json_insert_child(label, value);
25    json_insert_child(entry, label);
26  
27    // insert the second label-value pair
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);
32  
33    // inserts that object as a value in a label-value pair
34    label = json_new_string("entry");
35    json_insert_child(label, entry);
36  
37    // inserts that label-value pair into the root object
38    json_insert_child(root, label);
39  
40    // print the result
41    json_tree_to_string(root, &text);
42    printf("%s\n",text);
43  
44    // clean up
45    free(text);
46    json_free_value(&root);
47  
48//打印第二部分,数组示例,
49//结果:
50//    ["test1","test2",109]
51  
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);
59  
60        json_tree_to_string(root,&text);
61        printf("%s\n",text);
62  
63        // clean up
64        free(text);
65        json_free_value(&root);
66  
67    return EXIT_SUCCESS;
68}</STRONG>
【mjson例二】
01#include <stdio.h>
02#include <stdlib.h>
03#include <locale.h>
04  
05#include "json.h"
06  
07json_t *new_entry(char *name, char *phone)
08{
09    json_t *entry, *label, *value;
10  
11    // create an entry node
12    entry = json_new_object();
13      
14    // insert the first label-value pair
15    label = json_new_string("name");
16    value = json_new_string("Andew");
17    json_insert_child(label, value);
18    json_insert_child(entry, label);
19      
20    // insert the second label-value pair
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);
25  
26    // inserts that object as a value in a label-value pair
27    label = json_new_string("entry");
28    json_insert_child(label, entry);
29  
30    return label;
31}
32  
33int main (void)
34{
35    setlocale (LC_ALL, "");//设置为系统默认的地域信息
36  
37    json_t *root, *subtree;
38  
39    // creates the root node
40    root = json_new_object();
41  
42    // creates the desired MJSON document subtree
43    subtree = new_entry("Andrew", "555 123 456");
44  
45    // inserts the subtree into the root object
46    json_insert_child(root, subtree);
47  
48    // print the result
49    char *text;
50    json_tree_to_string(root, &text);
51    printf("%s\n",text);    //官方例子中为printf("%ls\n",text);去掉l才能打印出来。。
52  
53    // clean up
54    free(text);
55    json_free_value(&root);
56    return EXIT_SUCCESS;
57}
58  
59【输出结果】
60{"entry":{"name":"Andew","phone":"555 123 456"}}
【mjson例三】
01#include <stdio.h>
02#include <stdlib.h>
03#include <locale.h>
04  
05#include "json.h"
06  
07int main (void)
08{
09    setlocale (LC_ALL, "");
10    char *document = "{\"entry\":{\"name\":\"Andew\",\"phone\":\"555 123 456\"}}";
11  
12    json_t *root;
13  
14    printf("Parsing the document…\n");
15    root = json_parse_document(document);
16  
17    printf("Printing the document tree…\n");
18    json_tree_to_string(root, &document);
19    wprintf("%ls\n", document);
20  
21    // clean up
22    json_free_value(&root);
23    return EXIT_SUCCESS;
24}

【参考】

http://www.json.org/json-zh.html

http://mjson.sourceforge.net/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Crow 中,crow::json::rvalue 和 crow::json::wvalue 是两个不同的 JSON 类型,其中 crow::json::rvalue 是只读的 JSON 类型,而 crow::json::wvalue 是可写的 JSON 类型。 如果我们想要将 crow::json::wvalue 对象转换为 crow::json::rvalue 对象,可以直接将它们的引用进行转换,即: ```c++ const crow::json::rvalue& r = w; ``` 这里的 "w" 是一个 crow::json::wvalue 对象。 这种转换方式非常简单,因为 crow::json::rvalue 是 crow::json::wvalue 的基类,可以直接将 crow::json::wvalue 对象转换为 crow::json::rvalue 对象。 如果我们想要将 crow::json::rvalue 对象转换为 crow::json::wvalue 对象,则需要使用 crow::json::rvalue 类的成员函数 `dump()` 将其序列化为 JSON 字符串,然后使用 crow::json::load() 函数将其反序列化为一个 crow::json::wvalue 对象,例如: ```c++ const crow::json::rvalue& r = ...; std::string json_str = crow::json::dump(r); crow::json::wvalue w; crow::json::load(w, json_str); ``` 这里的 "r" 是一个 crow::json::rvalue 对象,`crow::json::dump(r)` 将其序列化为 JSON 字符串,然后使用 `crow::json::load()` 函数将其反序列化为一个 crow::json::wvalue 对象 "w"。 需要注意的是,由于 crow::json::wvalue 对象是可写的,因此在转换过程中可能会丢失一些信息。例如,如果 crow::json::rvalue 对象中包含一些只读的属性,这些属性在转换为 crow::json::wvalue 对象时可能会被忽略。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值