I was trying to convert one std::string to rapidJson object in below format
{
"data":{
"value": "AB1234"
}
}
I have tried
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 ;
This is giving output {"value":"ABCDEF"}
You forgot to create a Value
for "data"
:
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);
Output:
{
"data": {
"value": "ABCDEF"
}
}