#include#include "json/json.h" using namespace std; int main() { Json::Value root; root["name"] = "John"; root["age"] = 30; root["address"]["city"] = "New York"; root["address"]["state"] = "NY"; Json::StyledWriter writer; string output = writer.write(root); cout << output << endl; return 0; }
#includeIn this example, the code demonstrates how to output the JSON data to a file instead of the console. The code uses the StyledWriter class to format the JSON output before writing it to the file. Package library: CPP JSON Library (https://github.com/open-source-parsers/jsoncpp)#include "json/json.h" int main() { Json::Value root; root["name"] = "John"; root["age"] = 30; root["address"]["city"] = "New York"; root["address"]["state"] = "NY"; Json::StyledWriter writer; std::ofstream file("output.json"); file << writer.write(root); file.close(); return 0; }