std::string到rapidJson对象的转换
std::string to rapidJson object conversion
我试图将一个 std::string 转换为以下格式的 rapidJson 对象
{
"data":{
"value": "AB1234"
}
}
我试过了
rapidjson::Document aJsonDocument;
aJsonDocument.SetObject();
rapidjson::Document::AllocatorType &aAllocator = aJsonDocument.GetAllocator();
rapidjson::Value aPsmJson(rapidjson::kStringType);
std::string aStr = "ABCDEF";
aPsmJson.SetString(aStr.c_str(), aAllocator);
aJsonDocument.AddMember("value", aPsmJson, aAllocator);
//jsonToString is a function to convert json document to string
std::string aInputJsonString = jsonToString(aJsonDocument);
std::cout << "Output: " << aInputJsonString ;
这是输出
{"value":"ABCDEF"}
您忘记为 "data"
创建 Value
:
string s = "ABCDEF";
Document d(kObjectType);
Value data(kObjectType);
Value value;
value.SetString(s.c_str(), d.GetAllocator());
data.AddMember("value", value, d.GetAllocator());
d.AddMember("data", data, d.GetAllocator());
std::cout << jsonToString(d);
输出:
{
"data": {
"value": "ABCDEF"
}
}
我试图将一个 std::string 转换为以下格式的 rapidJson 对象
{
"data":{
"value": "AB1234"
}
}
我试过了
rapidjson::Document aJsonDocument;
aJsonDocument.SetObject();
rapidjson::Document::AllocatorType &aAllocator = aJsonDocument.GetAllocator();
rapidjson::Value aPsmJson(rapidjson::kStringType);
std::string aStr = "ABCDEF";
aPsmJson.SetString(aStr.c_str(), aAllocator);
aJsonDocument.AddMember("value", aPsmJson, aAllocator);
//jsonToString is a function to convert json document to string
std::string aInputJsonString = jsonToString(aJsonDocument);
std::cout << "Output: " << aInputJsonString ;
这是输出 {"value":"ABCDEF"}
您忘记为 "data"
创建 Value
:
string s = "ABCDEF";
Document d(kObjectType);
Value data(kObjectType);
Value value;
value.SetString(s.c_str(), d.GetAllocator());
data.AddMember("value", value, d.GetAllocator());
d.AddMember("data", data, d.GetAllocator());
std::cout << jsonToString(d);
输出:
{
"data": {
"value": "ABCDEF"
}
}