Как изменить данные JSON с помощью RapidJSON, изменив их в другой схеме - PullRequest
0 голосов
/ 15 мая 2019

Мне нужно изменить данные JSON в другую схему, используя RapidJson. Я могу изменить имя и значение объекта Value, но я не смог найти правильные примеры, чтобы изменить JSON, переставив узлы.

Я попытался создать новый документ и добавить значение, взятое из другого документа, но оно не работает.

    rapidjson::Document doc1, doc2;
            rapidjson::Document::AllocatorType& alloc = doc1.GetAllocator();
            doc1.Parse(str); // str contains the JSON data

            Value statusObj(doc1["status"], alloc);
            Value resultsObj(doc1["area_data"]["update_results"], alloc);

            doc2.SetObject();
            doc2.AddMember("status", statusObj, alloc);
            doc2.AddMember("results", resultsObj, alloc);

//doc1 - This is the inout JSON
{
    "status": {},
    "area_data":
    {
        ""
        "update_results":[]
    }
}

//doc2 - This is what am trying to create
{
    "status": {},
    "results":[]    //update_results from doc 1
}

1 Ответ

0 голосов
/ 16 мая 2019

Нашел способ сделать это. Это работает!

            rapidjson::Document doc, newDoc;
            rapidjson::Document::AllocatorType& alloc = doc.GetAllocator();
            doc.Parse(str); // str contains the JSON data

            Value& results = doc["area_data"]["update_results"];

            Value newResults(kArrayType);

            newResults.CopyFrom(results, alloc);

            doc.AddMember("results", newResults, alloc);
            doc.RemoveMember("area_data");
...