2015-10-07 97 views
0

我試圖刪除嵌套在JSON文件中的對象中的對象。但是,我無法在互聯網上或正式的rapidjson頁面上找到任何示例。我的代碼是用C++編寫的。使用rapidjson刪除JSON中的嵌套對象

我曾嘗試用下面的代碼:

const Value& rootObject= document["root"]; 

const Value& settingsObject = extensionsObject; 

settingsObject.RemoveMember(); 

但我不知道通過什麼參數或如何初始化MemberIterator的確切元素(因爲我已經知道我要刪除的對象的名稱)。

這裏是例如JSON結構:

{ 
    "root": { 
    "settings": { 
     "item1": { 
     "someInfo": 123 
     }, 
     "item2": { 
     "someInfo": "string" 
     } 
    } 
    } 
} 

回答

1

請赤我的代碼。

Value Child_Obj(kObjectType); // creat object to but it inside another object 
Child_Obj.SetObject(); // set it 

Child_Obj.AddMember("Child Number", Value(15), Document->GetAllocator()); // add to child key and its value 

Value Parent_Obj(kObjectType); // creat parent object that will have inside him the child object 
Parent_Obj.SetObject(); // set it 

Parent_Obj.AddMember("Parent Number", Value(10), Document->GetAllocator()); // add to parent key and its value 
Parent_Obj.AddMember("Child", Child_Obj, Document->GetAllocator()); // add child object to parent object , "Child" as key and Child_Obj as value 

// now the file looks like this : 
/* 
{ 
    "Parent": 
    { 
    "Parent Number":10, 
    "Child": 
    { 
     "Child Number":15 
    } 
    } 
} 
*/ 

// let delete this child 
Parent_Obj.RemoveMember("Child"); // here you will give it the key for the object you need to delete 

// now its look like this : 
/* 
{ 
    "Parent": 
    { 
    "Parent Number":10, 
    } 
} 
*/ 

// and for fun , if you want to iterate through object , you can do this : 
Value::MemberIterator it = Parent_Obj.MemberBegin(); 
for (; it != Parent_Obj.MemberEnd(); ++it) 
{ 
    std::string str = it->name.GetString(); // this will give you the key for current child 
}