美文网首页
c++ json序列化库Elson示例

c++ json序列化库Elson示例

作者: 一路向后 | 来源:发表于2022-02-16 22:15 被阅读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

相关文章

网友评论

      本文标题:c++ json序列化库Elson示例

      本文链接:https://www.haomeiwen.com/subject/drxjlrtx.html