以下のコードがクラッシュします (デバッグ エラー! R6010 abort() が呼び出されました)。助けてもらえますか? また、文字列値から json オブジェクトを初期化する方法も知りたいです。
Json::Value obj;
obj["test"] = 5;
obj["testsd"] = 655;
string c = obj.asString();
ベストアンサー1
こんにちは、それはとても簡単です:
1 - データを保存するにはCPP JSON値オブジェクト(Json::Value)が必要です
2 - Json リーダー (Json::Reader) を使用して JSON 文字列を読み取り、JSON オブジェクトに解析します。
3 - 自分の仕事をやりましょう :)
これらの手順を実行するための簡単なコードは次のとおりです。
#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;
}
コンパイルするには: g++ YourMainFile.cpp -o main -l jsoncpp
役に立つと嬉しいです ;)