在另一个中检索JSON对象

Retrieving a JSON object within another one

本文关键字:JSON 对象 检索 另一个      更新时间:2023-10-16

我有一个简单的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");