4

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"}

user2235747
  • 345
  • 6
  • 14
  • I have to send the std::string to a URI. The std string will contain the message like { "data":{ "value": "AB1234" } } So i was forming the json object and trying to convert it into std::string. – user2235747 Jun 12 '18 at 06:57
  • So, problem solved? – wp78de Jun 15 '18 at 23:21

1 Answers1

5

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"
    }
}
krisz
  • 2,686
  • 2
  • 11
  • 18