2013-05-26 64 views
0

正試圖使用​​android JSON對象來解析特定的響應,但無法制作解析此響應的代碼「{」r「:{」f「:[1,0,15 ,5948]}}」。Android JSONObject解析問題

使用以下代碼試過,但我得到錯誤:

「否對於f值:{」 R 「:{」 F 「:[1,0,15,5948]}}」

代碼如下:

String abc = "{\"r\":{\"f\":[1,0,15,5948]}}"; 

JSONObject json = new JSONObject(abc); 

if (json.has("r")) { 

Bundle b = new Bundle(); 

b.putInt("p", json.getJSONArray("f").getInt(0)); 

b.putInt("s", json.getJSONArray("f").getInt(1)); 

} 

我打算來解析上述反應和在束各個變量得到的值。 像b.putInt("p", json.getJSONArray("f").getInt(0));應該得到f中的1:[1 ....] 等等。

有人可以幫助有一個工作代碼獲取上述響應的值。

回答

2

"f""r"一個子元素,因此,你需要訪問它是這樣的:

getJSONObject("r").getJSONArray("f")

0

當你這樣做:

b.putInt("p", json.getJSONArray("f").getInt(0));

json變量仍然引用根對象你的json。您必須遍歷一個級別才能訪問字段f。

這個工作對我來說:

String abc = "{\"r\":{\"f\":[1,0,15,5948]}}"; 
JSONObject json = new JSONObject(abc); 
if (json.has("r")) { 
    json = json.getJSONObject("r"); 
    Bundle b = new Bundle(); 
    b.putInt("p", json.getJSONArray("f").getInt(0)); 
    b.putInt("s", json.getJSONArray("f").getInt(1)); 
} 

注行:

JSON = json.getJSONObject( 「R」);

+0

謝謝smuk,我會檢查是否幫助我獲得價值。 – user2422139