Json-cpp -如何从字符串初始化并获得字符串值?

时间:2021-08-06 01:16:29

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.

我下面的代码崩溃了(调试错误!调用R6010 abort()。你能帮我吗?我还想知道如何从字符串值初始化json对象。

Json::Value obj;
obj["test"] = 5;
obj["testsd"] = 655;
string c = obj.asString();

2 个解决方案

#1


23  

Hello it is pretty simple:

你好,很简单:

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

1 -您需要一个CPP JSON值对象(JSON:: value)来存储数据。

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

2 -使用Json阅读器(Json::Reader)读取Json字符串并解析为Json对象

3 - Do your Stuff :)

3 -做你的事:)

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

编译:g++ YourMainFile。cpp -o主-l jsoncpp

I hope it helps ;)

我希望它能有所帮助;

#2


0  

Json::Reader is deprecated, as stated in the documentation. Here's how to use Json::CharReader and Json::CharReaderBuilder:

Json::Reader被弃用,如文档中所述。下面是如何使用Json::CharReader和Json::CharReaderBuilder:

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's answer here: Parsing JSON string with jsoncpp

p-a-o-l-o的回答是:使用jsoncpp解析JSON字符串

#1


23  

Hello it is pretty simple:

你好,很简单:

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

1 -您需要一个CPP JSON值对象(JSON:: value)来存储数据。

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

2 -使用Json阅读器(Json::Reader)读取Json字符串并解析为Json对象

3 - Do your Stuff :)

3 -做你的事:)

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

编译:g++ YourMainFile。cpp -o主-l jsoncpp

I hope it helps ;)

我希望它能有所帮助;

#2


0  

Json::Reader is deprecated, as stated in the documentation. Here's how to use Json::CharReader and Json::CharReaderBuilder:

Json::Reader被弃用,如文档中所述。下面是如何使用Json::CharReader和Json::CharReaderBuilder:

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's answer here: Parsing JSON string with jsoncpp

p-a-o-l-o的回答是:使用jsoncpp解析JSON字符串