简体   繁体   English

使用RapidJSON删除JSON中的嵌套对象

[英]Remove nested object in JSON with rapidjson

I'm trying to remove object nested in object in JSON file. 我正在尝试删除嵌套在JSON文件中的对象中的对象。 However, I can not find any examples on the internet or on the official rapidjson page. 但是,我在互联网上或官方的Rapidjson页面上找不到任何示例。 My code is written on C++. 我的代码是用C ++编写的。

I have tried with the following code: 我尝试使用以下代码:

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

const Value& settingsObject = extensionsObject;

settingsObject.RemoveMember();

But I am not sure what parameter to pass or how to initialize MemberIterator for exact element (as I already know the name of the object I want to remove). 但是我不确定要传递哪个参数或如何为精确元素初始化MemberIterator(因为我已经知道要删除的对象的名称)。

Here is example of the JSON structure: 这是JSON结构的示例:

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

please chek my code. 请检查我的代码。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM