#include "rapidjson/document.h" #include "rapidjson/writer.h" #include "rapidjson/stringbuffer.h" using namespace rapidjson; int main() { Document document; document.SetObject(); // Add a string value to the JSON object Value key("name", document.GetAllocator()); Value value("John Doe", document.GetAllocator()); document.AddMember(key, value, document.GetAllocator()); // Print the resulting JSON object StringBuffer buffer; Writerwriter(buffer); document.Accept(writer); std::cout << buffer.GetString() << std::endl; // Output: {"name":"John Doe"} return 0; }
#include "rapidjson/document.h" #include "rapidjson/writer.h" #include "rapidjson/stringbuffer.h" using namespace rapidjson; int main() { Document document; document.SetObject(); // Add a nested JSON object to the JSON object Value key("address", document.GetAllocator()); Value address(kObjectType); Value street("123 Main St.", document.GetAllocator()); address.AddMember("street", street, document.GetAllocator()); Value city("Anytown", document.GetAllocator()); address.AddMember("city", city, document.GetAllocator()); Value state("CA", document.GetAllocator()); address.AddMember("state", state, document.GetAllocator()); Value zip("12345", document.GetAllocator()); address.AddMember("zip", zip, document.GetAllocator()); document.AddMember(key, address, document.GetAllocator()); // Print the resulting JSON object StringBuffer buffer; WriterThis example adds a new nested JSON object to the main JSON object, with values for a street address, city, state, and zip code. The resulting JSON object is then printed to the console.writer(buffer); document.Accept(writer); std::cout << buffer.GetString() << std::endl; // Output: {"address":{"street":"123 Main St.","city":"Anytown","state":"CA","zip":"12345"}} return 0; }