c语言如何解析100mb的json,C语言的JSON 解析库 - MJSON使用介绍

例子一:

#include

#include

#include

#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;

}例子二:

#include

#include

#include

#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"}}例子三:

#include

#include

#include

#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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值