gpt4 book ai didi

c++ - 使用 rapidjson 删除 JSON 中的嵌套对象

转载 作者:行者123 更新时间:2023-11-28 06:02:23 26 4
gpt4 key购买 nike

我正在尝试删除嵌套在 JSON 文件中的对象中的对象。但是,我在互联网或官方 rapidjson 页面上找不到任何示例。我的代码是用 C++ 编写的。

我试过以下代码:

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

const Value& settingsObject = extensionsObject;

settingsObject.RemoveMember();

但我不确定要传递什么参数或如何为确切的元素初始化 MemberIterator(因为我已经知道我要删除的对象的名称)。

这是 JSON 结构的示例:

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

最佳答案

请检查我的代码。

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
}

关于c++ - 使用 rapidjson 删除 JSON 中的嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32989698/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com