c++ json序列化库Elson示例

2022-02-16  本文已影响0人  一路向后

1.源码实现

#include <iostream>
#include <elson/Elson.hpp>

using namespace JSON;
using namespace std;

int main()
{
    Value val;
    Value eval;
    Parser p;

    val["Name"] = "Homer";
    val["Family"]["Wife"] = "Marge";
    val["Family"]["Kids"] = { "Bart", "Lisa", "Maggie" };
    val["Age"] = 40;

    PrettyPrinter printer;

    string str = printer.print(val);

    cout << str << endl;

    p.parse(eval, str);

    int answer = val["Age"].as<int>();

    cout << answer << endl;

    return 0;
}

2.编译源码

$ g++ -o test test.cpp -std=c++11

3.运行及其结果

$ ./test
{
    "Age": 40,
    "Family": {
        "Kids": ["Bart", "Lisa", "Maggie"],
        "Wife": "Marge"
    },
    "Name": "Homer"
}
40
上一篇 下一篇

猜你喜欢

热点阅读