ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

RepidJson将内容写入文件简单代码示例

RepidJson将内容写入文件简单代码示例

以下是使用RapidJSON将内容写入文件的示例代码:

#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include <iostream>
#include <fstream>using namespace rapidjson;int main() {// 创建一个json文档Document document;document.SetObject();Document::AllocatorType& allocator = document.GetAllocator();// 添加一些键值对document.AddMember("name", "John", allocator);document.AddMember("age", 30, allocator);// 将json文档的内容转换为字符串StringBuffer buffer;Writer<StringBuffer> writer(buffer);document.Accept(writer);// 将字符串写入文件中std::ofstream file("output.json");file << buffer.GetString();file.close();return 0;
}

运行程序后,会在当前目录下生成一个名为output.json的文件,其中包含以下内容:

{"name": "John","age": 30
}
返回列表