在另一个中检索JSON对象
Retrieving a JSON object within another one
我有一个简单的JSON格式文本文件,我在" Pixel"数组中检索值时遇到了一些麻烦。这是文件:
{ "luminaire" :
{ "sensors":
{ "pixel" : [2000,2001,2002] }
}
}
我为此写的代码是以下内容:
//After parsing success...
Json::Value pixel = root_["luminaire"].get("sensors" , "nothing").get("pixel" , "nopixel");
int value = pixel[0].asInt();
我尝试了多种方法来做到这一点,但是我一直遇到以下错误:
terminate called after throwing an instance of 'Json::LogicError'
what(): in Json::Value::operator[](ArrayIndex): requires arrayValue
我也尝试了
Json::Value:ArrayIndex and root[0]
但是我遇到了相同的错误。
如何在"像素"数组中检索值?
您不是说root_.get("luminaire")
吗?毕竟,luminaire
是一个关键名称,就像sensors
一样。
您需要使用root_.get("luminaire")
root_.get("luminaire").get("sensors" , "nothing").get("pixel" , "nopixel");
相关文章:
- 有没有办法让编译器在我放置字符串而不是 nlohmann::json 对象时抛出错误?
- 如何使用 picojson.h 在 c++ 中从头开始创建 json 对象
- .value( "key" , default) 不适用于空的 json 对象吗?
- Rapidjson 遍历并获取复杂 JSON 对象成员的值
- JSON对象到C++模型
- 使用 NLOHMANN JSON 在 C++ 中创建嵌套的 JSON 对象
- 如何在 c++ 中从两个向量创建 JSON 对象?
- 如何正确解析 Java+C++ TCP 连接中的 JSON 对象?
- 如何将矢量转换为 json 对象?C++
- 将C++具有 150+ 变量的类转换为 Json 对象
- Poco 迭代嵌套的 JSON 对象
- 如何提取由特定模式分开的JSON对象字符串
- 如何创建具有分层对象和值的 json 对象
- 将 JSON 对象作为参数在 JNI 中传递给 CPP,并在 CPP 中检索 JSON 对象的数据
- 无法区分 JSON 对象/数组/字符串
- 如何将 cpr 的获取响应转换为 json 对象?
- 我正在尝试打开一个 json 文件.并将其存储为 JSON 对象
- 如何在不更改 C++ 中的值的情况下更改 json 对象名称
- 如何将Nolhmann JSON对象分成字符串
- Cpp:Cpp中的JSON解析器,提供支持序列化/反序列化功能,将JSON对象转换为用户定义的类