C語言 JSON 解析庫 - MJSON使用介紹

工程師WWW發表於2016-12-14

安裝:
解壓出來,然後直接包含 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/


相關文章