C++:美化助推树json_parser
C++: Beautify boost ptree json_parser
在以下使用 Boost 属性树的代码中C++我希望有一个漂亮的输出,例如
{
"fruit": {
"apple": "true",
"orange": "true"
},
"animal": {
"cat": "true",
"dog": "true",
"bird": {
"duck": "true"
}
}
}
而实际上我收到:
{"fruit":{"apple":"true","orange":"true"},"animal":
{"cat":"true","dog":"true","bird":{"duck":"true"}}}
有没有内置方法来美化这个json
结果?
#include <iostream>
#include <string>
#include <sstream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
using boost::property_tree::ptree;
using boost::property_tree::basic_ptree;
int main()
{
ptree root;
root.put("fruit.apple", "true");
root.put("fruit.orange", "true");
root.put("animal.cat", "true");
root.put("animal.dog", "true");
root.put("animal.bird.duck", "true");
std::ostringstream buf;
write_json(buf, root, false);
buf << std::endl;
std::string json = buf.str();
std::cout<<json<<std::endl;
return 0;
}
您是否尝试过为pretty
参数传递true
?
write_json(buf, root, true);
相关文章:
- 在c++中使用nlohmann从类到json的转换
- C++json插入数组
- nlohmann-json将一个数组插入到另一个数组中
- 如何使用curlpp通过POST方法上传文件和json数据
- JSON转换为nlohmann JSON-lib中的结构数组
- NLOHMANN 的 JSON 库将数组转换为结构向量
- 在nlohmann json中,如何将嵌套对象的数组转换为嵌套结构的向量
- 如何在 nlohmann 的 json 库中获取数组长度?
- UE4 C++ 我无法将 JSON 嵌套值获取到 TArray
- 如何在特定位置追加 json 文件
- 有没有办法让编译器在我放置字符串而不是 nlohmann::json 对象时抛出错误?
- 使用 jsoncpp 解析 json 数组字符串
- 将 FlatBuffer 对象序列化为 JSON,而不使用其架构文件
- 如何在 c++ 非托管代码中反序列化 byte[] 的 json 字符串?
- 使用 cmake 生成 compile_commands.json
- 如何使用 c++ 和提升库生成 json
- 使用 minijson-reader 库读取 JSON 字符串时出现问题
- 使用 Qt 解析 JSON 数组
- Parser JSON ON QT
- C/C++ JSON parser