//main.cpp
#pragma
#include "main.h"
int main()
{
ptree pt;
pt.add("add", "add1");
pt.add("add", "add2");
pt.put("put", "put1");
pt.put("put", "put2");
ofstream file;
file.open("ptree.txt", ios::out);
std::ostringstream buf;
write_json(buf, pt, false);
string str = buf.str();
file << "新建的json文本" << endl;
file << str;
file.close();
return 0;
}
生成的ptree.txt文本内容:
新建的json文本
{"add":"add1","add":"add2","put":"put2"}
第二行换成标准json格式:
{
"add": "add1",
"add": "add2",
"put": "put2"
}
so:
- add 和 put 均是对 value 的操作,但是如果有相同的 key 值,add 会添加一个同级且 key 值相同的一个 value,而 put 会覆盖修改相同 key 值 value