#include <iostream>
#include <json\json.h>
#include <fstream>
using namespace std;
using namespace Json;
void init() {
// 将最外层的数组看做一个Value
// 最外层的Value对象创建
Value root;
// 创建并初始化子对象
Value subObj;
Value obj;
// 添加键值对
obj["streampath"] = "144.1.1.1";
obj["resolutionratio"] = "720p";
obj["refreshrate"] = "25hz";
subObj["1.1"] = obj;
root.append(subObj);
#if 1
// 有格式的字符串
string str = root.toStyledString();
#else
FastWriter f;
string str = f.write(root);
#endif
// 将序列化的字符串写磁盘文件
ofstream ofs("test1.json");
ofs << str;
ofs.close();
}
初始化后生成一个名为 test1.json 的文件内容为:
[
{
"1.1" :
{
"refreshrate" : "25hz",
"resolutionratio" : "720p",
"streampath" : "144.1.1.1"
}
}
]