mercredi 4 avril 2018

Rapidjson assign a value of key from one document object to other document object

I have two json strings parsed by Rapidjson library.

Json 1:

{
  "jKey1":{
           "jVal1Key1":{
                         "mystr":["str1","str2"]
                       },
           "jVal2Key2":["data1","data2"]
          },
  "jKey2":"Title"
}

Json 2:

{
    "jVal1Key1":{
                  "mystr":["str1","str2"]
                }
}

I just want to replace "jVal1Key1" of Json 1 with "jVal1Key1" of Json 2.

So I tried the following code.

Document doc1;
doc1.Parse<0>(json1.c_str()).HasParseError();
doc1.Parse(json1.c_str());

Document doc2;
doc2.Parse<0>(json2.c_str()).HasParseError();
doc2.Parse(json2.c_str());

if(doc1.HasMember("jKey1"))
{
    if(doc1["jKey1"].HasMember("jVal1Key1"))
    {
        if(doc2.HasMember("jVal1Key1"))
        {
           doc1["jKey1"]["jVal1Key1"] = doc2["jVal1Key1"];  // I failed here
        }
    }  
}

In my program, this below line,

doc1["jKey1"]["jVal1Key1"] = doc2["jVal1Key1"];  // I failed here

compiled successfully. But It fails at runtime. My question is, How can I copy the value of that key 'jVal1Key1' of 'doc2' to 'doc1'.

Aucun commentaire:

Enregistrer un commentaire