{
std::string result;
RapidJSON::Value json;
json.SetObject();
for(int i = 0; i < 5; i)
{
RapidJSON::Value data;
data.SetObject();
for(auto it = HashMap.begin(); it != HashMap.end(); it )
{
RapidJSON::Value arrObj;
arrObj.SetObject();
for(auto it2 = it->second.begin(); it2 != it->second.end(); it2 )
{
arrObj.AddMember(RapidJSON::StringRef(it2->first.c_str()), RapidJSON::StringRef(it2->second.c_str()), d.GetAllocator());
}
data.AddMember(RapidJSON::StringRef(it->first.c_str()), RapidJSON::Value(arrObj, d.GetAllocator()).Move(), d.GetAllocator());
}
json.AddMember(RapidJSON::StringRef(str.c_str()), RapidJSON::Value(data, d.GetAllocator()).Move(), d.GetAllocator());
}
cleanup:
retJson.AddToJson(RapidJSON::StringRef("STATUS"), json);
result = retJson.ToString();
}
I am using this method to convert a map to a json, but after each iteration of the outer for loop the value in the json value changes, the value assigned during the previous iteration becomes garbage values Can't find any solution, I have tried using creating a new Value and using the Move method, but that also didn't help
CodePudding user response:
don't use stringRef, instead create a copy of the strings in both arrObj, data member Sample code
RapidJSON::Value key(it2->first.c_str(), d.GetAllocator());
arrObj.AddMember(key, it2->second, d.GetAllocator());