преобразование объекта std :: string в rapidJson - PullRequest
0 голосов
/ 12 июня 2018

Я пытался преобразовать одну строку 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 "}

1 Ответ

0 голосов
/ 18 июня 2018

Вы забыли создать Value для "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);

Вывод:

{
    "data": {
        "value": "ABCDEF"
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...