在RapidJSON中解析数组内的对象 [英] Parsing object inside array in rapidjson
问题描述
我在实现递归函数时遇到问题,该函数遍历从json输入的解析中得到的树.
I'm having problems implementing a recursive function that goes over the tree I get from the parsing of a json input.
json输入.例如:
json input. e.g.:
{
"attr" : { "a": 1, "ovec": [ { "b": 2, "c": 3 }, { "d": 4} ] }
}
这就是我们所说的属性的复合值",该值只是一个JSON文档.它的内容是完全任意的(只要其有效的JSON).
This is what we call a 'compound value of an attribute', and the value is simply a JSON doc. Its content is completely arbitrary (as long as its valid JSON).
问题在于,使用Vector时,我必须使用Value :: ConstValueIterator类型进行循环(与Object不同,在这里我使用Value :: ConstMemberIterator).
The problem is that with a Vector I have to loop using the type Value::ConstValueIterator (unlike for Object, where I use Value::ConstMemberIterator).
我的递归函数将Value :: ConstMemberIterator作为参数,直到在向量中遇到Vector/Object为止一切正常-对于递归调用,我需要类型为Value :: ConstMemberIterator的迭代器.
My recursive function has Value::ConstMemberIterator as parameter and all is OK until I encounter a Vector/Object inside a Vector - for the recursive call I'd need an iterator of the type Value::ConstMemberIterator.
遍历"功能的相关部分:
Relevant parts of the "traversing" function:
int parseContextAttributeCompoundValue
(
const Value::ConstMemberIterator& node
)
{
std::string type = jsonParseTypeNames[node->value.GetType()];
if (type == "Array")
{
for (Value::ConstValueIterator iter = node->value.Begin(); iter != node->value.End(); ++iter)
{
std::string nodeType = jsonParseTypeNames[iter->value.GetType()];
if (nodeType == "String")
{
val = iter->GetString();
}
// else if ...
if ((nodeType == "Object") || (nodeType == "Array"))
{
// Here's my problem - need to convert 'iter' to Value::ConstMemberIterator
// in order to recursively call parseContextAttributeCompoundValue for this object/array
parseContextAttributeCompoundValue(iter); // COMPILATION ERROR
}
}
}
else if (type == "Object")
{
for (Value::ConstMemberIterator iter = node->value.MemberBegin(); iter != node->value.MemberEnd(); ++iter)
{
std::string nodeType = jsonParseTypeNames[iter->value.GetType()];
if (nodeType == "String")
{
val = iter->value.GetString();
}
else if (nodeType == "Number")
{
if ((nodeType == "Object") || (nodeType == "Array"))
{
// Here I'm just fine as iter is of the desired type already
parseContextAttributeCompoundValue(iter);
}
}
}
}
我已经尝试了一些操作,例如调用iter-> value.MemberBegin()以转换"为所需的类型,但到目前为止没有成功
I've tried a few things like calling iter->value.MemberBegin() to "convert" to the desired type, but so far without any success
非常感谢您在这里提供的帮助...
More than thankful for some help here ...
推荐答案
您可以简单地调用具有Value
类型的函数,而不用传递迭代器:
You can simply call a function with a Value
type, instead of passing iterator:
void parseContextAttributeCompoundValue(const Value& v) {
if (v.IsObject()) {
// ...
}
else if (v.IsArray() {
// ...
}
}
然后从呼叫站点:
for (Value::ConstValueIterator iter = ...) {
parseContextAttributeCompoundValue(*iter);
}
for (Value::ConstMemberIterator iter = ...) {
parseContextAttributeCompoundValue(iter->value);
}
这篇关于在RapidJSON中解析数组内的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!