如何使用整数作为键/名称将成员添加到rapidjson文档? [英] How can I add members to a rapidjson document using integers as the key/name?

查看:3878
本文介绍了如何使用整数作为键/名称将成员添加到rapidjson文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用for循环,并希望使用迭代器,i,作为键/名称当我添加一个成员到文档。例如,我想让文档看起来像这样:



{1:123.321,2:456.654}



这是我到目前为止所尝试的。



1。将i转换为const char *

  rapidjson :: Value newDouble(6); 
for(int i = 0; i< laserScan.size(); i ++){
newDouble.SetDouble(laserScan [i]);
const char * index = std :: to_string(i).c_str();
d.AddMember(index,newDouble,d.GetAllocator());
}

这会产生编译器错误,告诉我AddMember只能接受类型rapidjson的参数:: GenericValue&:

 错误:没有匹配的函数调用'rapidjson :: GenericDocument< rapidjson :: UTF8& > :: AddMember(const char *& rapidjson :: Value& rapidjson :: MemoryPoolAllocator<&)'
d.AddMember(index,newDouble,d.GetAllocator());将此名称 - 值对添加到JSON字符串

2。使用rapidjson类型将i转换为字符串

  rapidjson :: Value newDouble(6),newStringIndex 
for(int i = 0; i< laserScan.size(); i ++){
newDouble.SetDouble(laserScan [i]);
const char * index = std :: to_string(i).c_str();
size =(rapidjson :: SizeType)std :: strlen(index);
newStringIndexSetString(rapidjson :: StringRef(index,size));
d.AddMember(newStringIndex,newDouble,d.GetAllocator());
}

这会从Writer类抛出以下运行时错误:

 断言`!hasRoot_'失败。 

为什么我困惑


$

  d.AddMember( 1,newDouble,d.GetAllocator()); 

当我尝试它时,这是工作,但我不知道为什么将整数转换为const char *不会。

解决方案

我发现了一个解决方法。而不是使所有的键整数,我只是添加关键索引与相应的值是所有索引的数组。然后我添加了另一个名为data的数组,其中包含所有数据的数组:

  rapidjson :: Document document; 
rapidjson :: Document :: AllocatorType& allocator = document.GetAllocator();
rapidjson :: Value dataArray(rapidjson :: kArrayType),;

for(int i = 0; i dataArray.PushBack(rapidjson :: Value()。SetDouble(laserScan [i]),allocator );
}
document.AddMember(data,dataArrary,allocator);


I'm using a for loop and want to use the iterator, i, as the key/name when I add a member to the document. For example I want the document to look like this:

{"1":"123.321","2":"456.654"}

Here is what I have tried so far.

1. Converting i to a const char*

rapidjson::Value newDouble(6);
for(int i = 0;i<laserScan.size();i++){
    newDouble.SetDouble(laserScan[i]);
    const char* index = std::to_string(i).c_str();
    d.AddMember(index,newDouble,d.GetAllocator());
}

This generates a compiler error telling me that AddMember can only take arguments of type rapidjson::GenericValue&:

error: no matching function for call to ‘rapidjson::GenericDocument<rapidjson::UTF8<> >::AddMember(const char*&, rapidjson::Value&, rapidjson::MemoryPoolAllocator<>&)’
     d.AddMember(index,newDouble,d.GetAllocator());//add this name-value pair to the JSON string

2. Converting i to a string using rapidjson types

rapidjson::Value newDouble(6), newStringIndex(5);
for(int i = 0;i<laserScan.size();i++){    
    newDouble.SetDouble(laserScan[i]);
    const char* index = std::to_string(i).c_str();
    size = (rapidjson::SizeType)std::strlen(index);
    newStringIndex.SetString(rapidjson::StringRef(index,size));
    d.AddMember(newStringIndex,newDouble,d.GetAllocator());
}

This throws the following run-time error from Writer class:

Assertion `!hasRoot_' failed.

Why I'm Confused

Shouldn't solution #1 be the same thing as doing the following?

d.AddMember("1",newDouble,d.GetAllocator());    

This works when I try it, but I can't figure out why converting an integer to a const char* won't.

解决方案

I found a workaround. Instead of making all the keys integers, I just added the key "indices" with the corresponding value being an array of all the indices. Then I added another array called "data" which contained an array of all the data:

rapidjson::Document document;
rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
rapidjson::Value dataArray(rapidjson::kArrayType), ;

for(int i = 0;i<laserScan.size();i++){                 
    dataArray.PushBack(rapidjson::Value().SetDouble(laserScan[i]),allocator);
}
document.AddMember("data",dataArrary,allocator);

这篇关于如何使用整数作为键/名称将成员添加到rapidjson文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆