C ++ Boost Library - 尝试获取我的属性树的正确格式

时间:2017-02-04 19:15:26

标签: c++ json boost

我正在尝试使用属性树编写Json。这就是我的尝试:

boost::property_tree::propTree propTree1, propTree2, propTree3, propTree4, propTree5, propTree6, propTree7, propTree8, propTree9, propTree10;


propTree1.put( "a", "" );
propTree1.put( "b", "" );
propTree1.put( "c", "" );
propTree2.push_back(std::make_pair("", propTree1)); 

propTree3.put("l", "");
propTree3.put("m", "");
propTree4.push_back(std::make_pair("", propTree3));
propTree1.add_child("Msg", propTree4);

propTree5.put("r", "");
propTree5.put("s", "");
propTree6.push_back(std::make_pair("", propTree5));

propTree1.add_child("Msg1", propTree6);
propTree1.add_child("Msg2", propTree8);

propTree9.put("t", "");
propTree9.put("u", "");
propTree10.push_back(std::make_pair("", propTree9));
propTree1.add_child("Msg3", propTree10);

boost::property_tree::write_json( "OUTPUT", propTree1 );    

我得到的输出如下:

{
"a": "",
"b": "",
"Msg": [
    {
        "l": "",
        "m": ""
    }
],
"Msg1": [
    {
        "r": "",
        "s": ""
    }
],
"Msg2": "",
"Msg3": [
    {
        "t": "",
        "u": ""
    }
]

}

预期输出低于:(评论是问题)

{  "Msg0":{     //Missing in my output
"a": "",
"b": "",
"Msg": [
    {
        "l": "",
        "m": ""
    }
],
"Msg1": [
    {
        "r": "",
        "s": "",
    "abc": [    // Missing this "abc" array within Msg1 array
    "note1", //Output with values (no keys like "r" & "s")
    "note2",  // Can we pass setlist/stringstream in "abc" array?
    "note3",
    ]
    }
],
"Msg2": "",
"Msg3": [
    {
        "t": "",
        "u": ""
    }
]

} }

1)我无法获得上面提到的JSON格式。请查看评论。

2)我应该如何在" abc"中添加字符串/ stringstream / setofnames?阵列?哪种数据类型可以在这里传递以及如何实现?

3)还有什么有效的方法可以完成上述工作吗?

任何人都可以帮我解决上述3个问题吗?感谢。

0 个答案:

没有答案