gpt4 book ai didi

c++ - 在 rapidjson 中使用 AddMember 获取空值

转载 作者:行者123 更新时间:2023-11-28 01:51:46 25 4
gpt4 key购买 nike

我正在尝试使用 RapidJson 和 VC++ 创建 json 字符串。下面是我的代码:

WDocument saveDoc; 
WValue settingDoc(kObjectType);
settingDoc.AddMember("BOOK_ID", "456723", saveDoc.GetAllocator());
settingDoc.AddMember("BOOK_FOUND", true, saveDoc.GetAllocator());
WValue wImplVal;
wImplVal.SetString(BookName, saveDoc.GetAllocator()); //TCHAR BookName[MAX_PATH]
settingDoc.AddMember("BOOK_NAME", wImplVal, saveDoc.GetAllocator()); //gets correct book name
settingDoc.AddMember("BOOK_NICK_NAME",wImplVal,saveDoc.GetAllocator());//gets null value in wImplVal
saveDoc.SetObject();
saveDoc.AddMember("BOOK_INFO", settingDoc, saveDoc.GetAllocator());

我第二次得到 NULL 值,在使用 wImplVal 时,即使我没有修改它。rapidjson中两个键加一个值有什么特殊规则吗?提前致谢。

最佳答案

如果你仔细阅读 this,尤其是“移动语义”部分,你会发现 rapidjson 值使用移动语义,所以当你将它传递给 AddMember 时,它会移出现有值并将其替换为 Null

As RapidJSON supports C++03, it adopts move semantics using assignment operator, and all other modifying function like AddMember(), PushBack().

所以你必须重新设置值才能得到你想要的。

WDocument saveDoc; 
WValue settingDoc(kObjectType);
settingDoc.AddMember("BOOK_ID", "456723", saveDoc.GetAllocator());
settingDoc.AddMember("BOOK_FOUND", true, saveDoc.GetAllocator());
WValue wImplVal;
wImplVal.SetString(BookName, saveDoc.GetAllocator()); //TCHAR BookName[MAX_PATH]
settingDoc.AddMember("BOOK_NAME", wImplVal, saveDoc.GetAllocator()); //gets correct book name
wImplVal.SetString(BookName, saveDoc.GetAllocator());
settingDoc.AddMember("BOOK_NICK_NAME",wImplVal,saveDoc.GetAllocator());//gets null value in wImplVal
saveDoc.SetObject();
saveDoc.AddMember("BOOK_INFO", settingDoc, saveDoc.GetAllocator());

关于c++ - 在 rapidjson 中使用 AddMember 获取空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42779790/

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