| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx

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

0
HTML JSON C/C++ Go 44455 次浏览

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

 

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

 

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

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

【mjson例一】

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

#include "json.h"

int main (void)
{
    char *text;
    json_t *root, *entry, *label, *value;
    setlocale (LC_ALL, "");//设为系统默认地域信息

    // creates the root node
    root = json_new_object();
    // create an entry node
    entry = json_new_object();

//    第一部分,打印结果:
//    {"entry":{"name":"Andew","phone":"555 123 456"}}

    // insert the first label-value pair
    label = json_new_string("name");
    value = json_new_string("Andew");
    json_insert_child(label, value);
    json_insert_child(entry, label);

    // insert the second label-value pair
    label = json_new_string("phone");
    value = json_new_string("555 123 456");
    json_insert_child(label, value);
    json_insert_child(entry, label);

    // inserts that object as a value in a label-value pair
    label = json_new_string("entry");
    json_insert_child(label, entry);

    // inserts that label-value pair into the root object
    json_insert_child(root, label);

    // print the result
    json_tree_to_string(root, &text);
    printf("%s\n",text);

    // clean up
    free(text);
    json_free_value(&root);

//打印第二部分,数组示例,
//结果:
//    ["test1","test2",109]

        root = json_new_array();
        label = json_new_string("test1");
        json_insert_child(root,label);
        value = json_new_string("test2");
        json_insert_child(root,value);
        value = json_new_number("109");
        json_insert_child(root,value);

        json_tree_to_string(root,&text);
        printf("%s\n",text);

        // clean up
        free(text);
        json_free_value(&root);

    return EXIT_SUCCESS;
}
【mjson例二】
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

#include "json.h"

json_t *new_entry(char *name, char *phone)
{
    json_t *entry, *label, *value;

    // create an entry node
    entry = json_new_object();
    
    // insert the first label-value pair
    label = json_new_string("name");
    value = json_new_string("Andew");
    json_insert_child(label, value);
    json_insert_child(entry, label);
    
    // insert the second label-value pair
    label = json_new_string("phone");
    value = json_new_string("555 123 456");
    json_insert_child(label, value);
    json_insert_child(entry, label);

    // inserts that object as a value in a label-value pair
    label = json_new_string("entry");
    json_insert_child(label, entry);

    return label;
}

int main (void)
{
    setlocale (LC_ALL, "");//设置为系统默认的地域信息

    json_t *root, *subtree;

    // creates the root node
    root = json_new_object();

    // creates the desired MJSON document subtree
    subtree = new_entry("Andrew", "555 123 456");

    // inserts the subtree into the root object
    json_insert_child(root, subtree);

    // print the result
    char *text;
    json_tree_to_string(root, &text);
    printf("%s\n",text);    //官方例子中为printf("%ls\n",text);去掉l才能打印出来。。

    // clean up
    free(text);
    json_free_value(&root);
    return EXIT_SUCCESS;
}

【输出结果】
{"entry":{"name":"Andew","phone":"555 123 456"}}
【mjson例三】
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

#include "json.h"

int main (void)
{
    setlocale (LC_ALL, "");
    char *document = "{\"entry\":{\"name\":\"Andew\",\"phone\":\"555 123 456\"}}";

    json_t *root;

    printf("Parsing the document…\n");
    root = json_parse_document(document);

    printf("Printing the document tree…\n");
    json_tree_to_string(root, &document);
    wprintf("%ls\n", document);

    // clean up
    json_free_value(&root);
    return EXIT_SUCCESS;
}

【参考】

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

http://mjson.sourceforge.net/


来自:http://www.linuxpig.com/2010/12/yigecjsonjiexiku-mjsonjieshaoheshili/

8个答案

0

Are you looking for a reputable game portal to play card games? Choose Nhà cái uy tín to have the most interesting and best experiences. Game Paradise is considered the world of card games. What is the truth, let's discover with us at Nhà cái uy tín

0

Thank you for sharing this great article. 888b hopes you will have many more articles for everyone to read.

0
0

I learned absolutely everything about my question when I read this post, thanks to the author for the detailed description. I wrote my review on the freeessaywriter.net review you can go in and read. Thank you very much for your attention and your time.

0

I give more time to these programming languages because I know that there are many upcoming things to help through this. I order essay online so that I get an idea about the working and how world-changing with it.

0
不错。
0
不错,不错。
0
非常不错,了解下。