C++ RapidJSON 库通过其索引获取数组中的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10037778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RapidJSON library getting a value inside an array by its index
提问by Grego
{"hi": "hellow",
"first":
{"next":[
{"key":"important_value"}
]
}
}
Accessing RapidJSON inside array:
访问数组内的 RapidJSON:
this works: cout << "HI VALUE:" << variable["hi"].GetString() << endl;
this will output: hellow
as expected, the problem is to access inside values like if I want to get "Important_Value", I tried something like this: cout << "Key VALUE:" << variable["first"]["next"][0]["key"].GetString() << endl ;
but this doesn't work, I want to be able to get the "important_value" by the first item of the array, and in this case it's the [0]
that is causing error.
这有效:cout << "HI VALUE:" << variable["hi"].GetString() << endl;
这将输出:hellow
正如预期的那样,问题是访问内部值,例如我想获得“Important_Value”,我尝试了这样的事情:cout << "Key VALUE:" << variable["first"]["next"][0]["key"].GetString() << endl ;
但这不起作用,我希望能够获得“important_value” " 通过数组的第一项,在这种情况下,它[0]
是导致错误的原因。
How do I do to get it by its index? I hope it's clear my explanation.
我该如何通过它的索引来获取它?我希望我的解释很清楚。
Thanks in advance.
提前致谢。
回答by mola10
JSON
JSON
{"hi": "hellow", "first": {"next":[{"key":"important_value"} ] } }
Code:
代码:
rapidjson::Document document;
if (document.Parse<0>(json).HasParseError() == false)
{
const Value& a = document["first"];
const Value& b = a["next"];
// rapidjson uses SizeType instead of size_t.
for (rapidjson::SizeType i = 0; i < b.Size(); i++)
{
const Value& c = b[i];
printf("%s \n",c["key"].GetString());
}
}
Will print important_value
将打印important_value
回答by Milo Yip
[Update]
[更新]
By clever work of contributors, RapidJSON can now disambiguate literal 0
from string. So the issue is no longer happens.
通过贡献者的聪明工作,RapidJSON 现在可以0
从字符串中消除文字歧义。所以这个问题不再发生。
https://github.com/miloyip/rapidjson/issues/167
https://github.com/miloyip/rapidjson/issues/167
The problem, as mjean pointed out, the compiler is unable to determine whether it should call the object member accessor or the array element accessor, by literial 0
:
问题,正如 mjean 指出的那样,编译器无法通过文字确定它是否应该调用对象成员访问器或数组元素访问器0
:
GenericValue& operator[](const Ch* name)
GenericValue& operator[](SizeType index)
Using [0u]
or [SizeType(0)]
can workaround this.
使用[0u]
或[SizeType(0)]
可以解决此问题。
Another way to cope with this problem is stop using overloaded version for operator[]. For example, using operator()
for one type of access. Or using normal functions, e.g GetMember()
, GetElement()
. But I do not have preference on this right now. Other suggestions are welcome.
解决这个问题的另一种方法是停止使用 operator[] 的重载版本。例如,operator()
用于一种类型的访问。或者使用普通函数,例如GetMember()
, GetElement()
。但我现在对此没有偏好。欢迎其他建议。
回答by mjean
I noticed this in the tutorial.cpp file;
我在 tutorial.cpp 文件中注意到了这一点;
// Note:
//int x = a[0].GetInt(); // Error: operator[ is ambiguous, as 0 also mean a null pointer of const char* type.
int y = a[SizeType(0)].GetInt(); // Cast to SizeType will work.
int z = a[0u].GetInt(); // This works too.
I didnt test it but you may want to try one of these;
我没有测试过,但您可能想尝试其中之一;
variable["first"]["next"][0u]["key"].GetString()
变量["第一个"]["下一个"][0u]["键"].GetString()
variable["first"]["next"][SizeType(0)]["key"].GetString()
变量["第一个"]["下一个"][SizeType(0)]["key"].GetString()
回答by mohakagr
If you want to access it with brackets, then you can use the following:
如果你想用括号访问它,那么你可以使用以下内容:
int i=0;
cout<<"Key VALUE:"<<variable["first"]["next"][i]["key"].GetString()<<endl ;
Output: Key VALUE:important_value
输出:键值:important_value
It worked for me.
它对我有用。