c++ - Json-cpp - how to initialize from string and get string value? -
my code below crashes(debug error! r6010 abort() has been called). can me? i'd know how initialize json object string value.
json::value obj; obj["test"] = 5; obj["testsd"] = 655; string c = obj.asstring();      
hello pretty simple:
1 - need cpp json value object (json::value) store data
2 - use json reader (json::reader) read json string , parse json object
3 - stuff :)
here simple code make 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 quotes      json::value root;        json::reader reader;     bool parsingsuccessful = reader.parse( strjson.c_str(), root );     //parse process     if ( !parsingsuccessful )     {         std::cout  << "failed parse"                << reader.getformattederrormessages();         return 0;     }     std::cout << root.get("mykey", "a default value if not exists" ).asstring() << std::endl;     return 0; }   to compile: g++ yourmainfile.cpp -o main -l jsoncpp
i hope helps ;)
Comments
Post a Comment