2016-12-16 208 views
0

我有一個簡單的JSON格式的文本文件,我有一些麻煩檢索「像素」數組中的值。 以下是文件:檢索另一個JSON對象

{ "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] 

但我得到同樣的錯誤。
如何檢索「像素」數組中的值?

+0

哦,是正確的,現在工作的感謝! – iMadz

回答

0

難道你的意思是root_.get("luminaire")?畢竟,luminaire是一個關鍵名稱,就像sensors

0

您需要使用root_.get("luminaire")

root_.get("luminaire").get("sensors" , "nothing").get("pixel" , "nopixel");