我在使用boost庫,C++中的屬性樹創建json數組時遇到了麻煩。使用boost創建json數組property_tree
我把作爲參考this線程,尤其是這部分
ptree pt;
ptree children;
ptree child1, child2, child3;
child1.put("childkeyA", 1);
child1.put("childkeyB", 2);
child2.put("childkeyA", 3);
child2.put("childkeyB", 4);
child3.put("childkeyA", 5);
child3.put("childkeyB", 6);
children.push_back(std::make_pair("", child1));
children.push_back(std::make_pair("", child2));
children.push_back(std::make_pair("", child3));
pt.put("testkey", "testvalue");
pt.add_child("MyArray", children);
write_json("test2.json", pt);
結果:
{
"testkey": "testvalue",
"MyArray":
[
{
"childkeyA": "1",
"childkeyB": "2"
},
{
"childkeyA": "3",
"childkeyB": "4"
},
{
"childkeyA": "5",
"childkeyB": "6"
}
]
}
但我能做些什麼,如果我想實現只是簡單的陣列不含任何對象?像這樣:
[
{
"childkeyA": "1",
"childkeyB": "2"
},
{
"childkeyA": "3",
"childkeyB": "4"
},
{
"childkeyA": "5",
"childkeyB": "6"
}
]
非常感謝。支持JSON
如果你使用'pt.add_child( 「」,孩子們)會發生什麼;'? – NathanOliver
應用程序崩潰,因爲它試圖將孩子添加到空白部分,不幸的是.. 類似'pt.add_child(NULL,children);' –