This task is very easy using Json::Value's implicit constructors.
In the following example I created on the fly the configuration for a Minimizer object I wanted to include in a simulation:
#include <iostream> #include "jsoncpp/json.h" using namespace std; int main(int argc, char **argv) { // Creation Json::Value minimizer; Json::Value minParameters; minParameters["MinimumRMS"] = 0.2; minParameters["sgbUpdated"] = true; minimizer["Minimizer"]["parameters"] = minParameters; minimizer["algorithm"] = "TruncatedNewton"; // Output to see the result cout<<"creating nested Json::Value Example pretty print: " <<endl<<minimizer.toStyledString() <<endl; return 0; }Compile it doing:
$ g++ -o test_create_json test_create_json.cpp -ljson_linux-gcc-4.4.5_libmtAnd you'll get:
$ ./test_create_json creating nested Json::Value Example pretty print: { "Minimizer" : { "parameters" : { "MinimumRMS" : 0.20, "sgbUpdated" : true } }, "algorithm" : "TruncatedNewton" }
Thanks!
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHi,
ReplyDeleteIs there any way to not allow jsoncpp reoeder the entries on the basis of name ??
Hello,
DeleteWhat would you like to do?
Hi,
ReplyDeleteI am new in creating JSON objects using C++.
is there any way to create json object dynamically ?
I want to create below output JSON using JSONCPP.
{
"algorithm" : "TruncatedNewton",
"Developer" : "XYZ",
"SupportTeam" : "XYZ"
},
{
"algorithm" : "Database",
"Developer" : "ABC",
"SupportTeam" : "ABC"
}
...
...
...
My output will be different every-time like sometime there should be 2 values or sometime there should be 3 or many...
Can anyone help me how to achieve it using JSONCpp Json::Value?
Hi,
Deletedid you get the solution to print values in iteration?
Thank you for your post - it was very well easy to understand, and gave me the results I wanted.
ReplyDelete