24

My code below crashes(Debug Error! R6010 abort() has been called). Can you help me? I'd also would like to know how to initialize the json object from a string value.

Json::Value obj;
obj["test"] = 5;
obj["testsd"] = 655;
string c = obj.asString();
Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
Greyshack
  • 1,901
  • 7
  • 29
  • 48

3 Answers3

41

Hello it is pretty simple:

1 - You need a CPP JSON value object (Json::Value) to store your data

2 - Use a Json Reader (Json::Reader) to read a JSON String and parse into a JSON Object

3 - Do your Stuff :)

Here is a simple code to make those 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;
}

To compile: g++ YourMainFile.cpp -o main -l jsoncpp

I hope it helps ;)

Irineu Antunes
  • 779
  • 7
  • 9
27

Json::Reader is deprecated. Use Json::CharReader and Json::CharReaderBuilder instead:

std::string strJson = R"({"foo": "bar"})";

Json::CharReaderBuilder builder;
Json::CharReader* reader = builder.newCharReader();

Json::Value json;
std::string errors;

bool parsingSuccessful = reader->parse(
    strJson.c_str(),
    strJson.c_str() + strJson.size(),
    &json,
    &errors
);
delete reader;

if (!parsingSuccessful) {
    std::cout << "Failed to parse the JSON, errors:" << std::endl;
    std::cout << errors << std::endl;
    return;
}

std::cout << json.get("foo", "default value").asString() << std::endl;

Kudos to p-a-o-l-o for their answer here: Parsing JSON string with jsoncpp

Matias Kinnunen
  • 7,828
  • 3
  • 35
  • 46
  • 23
    Thanks for this update. It never ceases to amaze me how simple code gets deprecated and replaced by 3x complexity.. Cheers. – BoeroBoy Jan 15 '19 at 16:09
  • Documentation link is broken; now at: http://open-source-parsers.github.io/jsoncpp-docs/doxygen/index.html – Technophile Mar 11 '21 at 17:59
0

You can avoid using Json::CharReader and Json::CharReaderBuilder by using stringstream instead.

#include <string>
#include <sstream>
#include "jsoncpp/json/json.h"
    
int main() {
        std::string strJson = "{\"mykey\" : \"myvalue\"}"; 
        Json::Value obj;
    
        // read a JSON String
        stringstream(strJson) >> obj;
    
        // get string value
        std::string value1 = obj["mykey"].asString();

        // or to get a default value if it isn't set
        std::string value2 = obj.get("mykey", "...").asString();
        return 0;
}
max68
  • 31
  • 3