在Rapidjson中使用AddMember时获得无效的价值

Getting Null value on using AddMember in rapidjson

本文关键字:无效 Rapidjson AddMember      更新时间:2023-10-16

我正在尝试使用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());

我第二次获得无效的值,即使我没有修改它,也可以使用Wimplval。我们是否有一些特殊规则可以在Rapidjson中的两个键中添加一个值。预先感谢。

如果您仔细阅读此内容,尤其是"移动语义"部分,您可以确定Rapidjson Value Ass使用的,嗯,移动语义,因此当您将其传递给Null时,它会移动现有值并用CC_3。

当Rapidjson支持C 03时,它使用分配操作员采用移动语义,以及所有其他修改功能(如AddMember)(),buthback()。

因此,您必须再次设置价值才能获得想要的东西。

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());