Hi, this is pretty simple:
1 - to store data you need a CPP JSON value object (Json :: Value).
2 - Use the Json Reader (Json :: Reader) to read the JSON string and parse the JSON object
3 - Make your stuff :)
Here is a simple code for these steps:
#include <stdio.h> #include <jsoncpp/json/json.h> #include <jsoncpp/json/reader.h> #include <jsoncpp/json/writer.h> #include <jsoncpp/json/value.h> #include <string> int main( int argc, const char* argv[] ) { std::string strJson = "{\"mykey\" : \"myvalue\"}"; // need escape the quotes Json::Value root; Json::Reader reader; bool parsingSuccessful = reader.parse( strJson.c_str(), root ); //parse process if ( !parsingSuccessful ) { std::cout << "Failed to parse" << reader.getFormattedErrorMessages(); return 0; } std::cout << root.get("mykey", "A Default Value if not exists" ).asString() << std::endl; return 0; }
Compile: g ++ YourMainFile.cpp -o main -l jsoncpp
I hope this helps;)
Irineu antunes
source share