`doc.AddMember(“key1”,1,document.GetAllocator())`和`doc [“key1”] = 1`有什么区别?

时间:2015-04-23 02:20:35

标签: cocos2d-x-3.0 rapidjson

我想在cocos2d-x 3.4中使用rapidjson创建一个json对象,并将其转换为字符串:

rapidjson::Document doc;
doc.SetObject();
doc.AddMember("key1",1,doc.GetAllocator());
doc["key2"]=2;

rapidjson::StringBuffer sb;
rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
doc.Accept(writer);

CCLOG("%s",sb.GetString());

但输出为{"key1":1} not {"key1":1,"key2":2},为什么?

1 个答案:

答案 0 :(得分:0)

在RapidJSON的旧版(0.1x)中,var result = personOneList.Join ( personTwoList, person1 => new { Key1 = person1.Name, Key2 = person1.Surname }, person2 => new { Key1 = person2.FirstName, Key2 = person2.LastName }, (person1, person2) => new PersonOnePersonTwo { PersonOneId = person1.Id, PersonTwoId = person2.Id } ).ToList(); 返回表示Null的doc["key2"]单例。 Value实际上是写给那个单身人士。

newer versions of RapidJSON(v1.0.x)中,此行为已更改。它基本上使得在JSON对象中找不到的键的断言失败,以便解决您提到的确切问题。

提醒一下,当某个操作可能需要分配内存时(例如doc["key2"] = 2AddMember),必须出现PushBack个对象。由于Allocator通常只有一个参数,它不能像在STL中那样添加新成员。这很奇怪而且不是非常用户友好,但这是RapidJSON设计中性能和内存开销的权衡。

相关问题