我正在使用rapid json https://rapidjson.org/阅读一个json文件,并使用windows应用程序上的cpp使用visual studio 2017将其写入另一个json文件。
文件阅读代码
FILE* fp = fopen(inputJson, "rb"); // non-Windows use "r"
char readBuffer[65536];
FileReadStream is(fp, readBuffer, sizeof(readBuffer));
Document d;
d.ParseStream(is);
fclose(fp);
std::string json_str(readBuffer);
字符串
文件写入代码
FILE* fp2 = fopen(outputJson, "wb"); // non-Windows use "w"
char writeBuffer[65536];
FileWriteStream os(fp2, writeBuffer, sizeof(writeBuffer));
Writer<FileWriteStream> writer(os);
d.Accept(writer);
fclose(fp2);
型
但在目标文件中,缩进全部丢失。
源文件
{
"name" : "test_rapid",
"rect_obj":
[
{ "name": "high school mathematics",
"price": 12,
"fg_color":
{
"input_type": "val_map_c",
"input":
{
"measname": "MetaData_eOrigin_c",
"values": [0],
"mapping": ["white"],
"out_of_range" : "white"
}
}
},
型
目标文件中的缩进丢失
{"name":"test_rapid","rect_obj":[{"name":"high school mathematics","price":12,"fg_color":{"input_type":"val_map_c","input":{
型
如何保持缩进?
1条答案
按热度按时间amrnrhlw1#
为了保持缩进,需要使用
PrettyWriter
而不是Writer
:字符串