2011-08-30 42 views
0

我正在JSONObject中獲取json webservice的結果。打印這個jsonObject時,它會打印出精確的結果。我在,因爲我讀一個複雜的反應是在JSON遍歷到在android中使用Json webservices元素

{"FoodMenuRS" 
:{"Status":"Success", 
"TotalResults":2, 
"Results":{"Items": 
{"Item":[ 
{"@Id":"6","@Name":"Tea"}, 
{"@Id":"4","@Name":"Coffee"} 
]}}}} 

我在這裏讀通過的形式,這個結果獲取值的時間有問題,

JSONArray jsonArray = json.getJSONArray("Item"); 

我在這裏得到錯誤「沒有價值項目」

凡爲同時呼籲其他服務,這是格式簡單,我得到獲取價值,

{"earthquakes": 
[{"eqid":"c0001xgp","magnitude":8.8,"lng":142.369,"src":"us","datetime":"2011-03-11 04:46:23","depth":24.4,"lat":38.322}, 
{"eqid":"2010xkbv","magnitude":7.5,"lng":91.9379,"src":"us","datetime":"2010-06-12 17:26:50","depth":35,"lat":7.7477}]} 

我把它叫做使用,

JSONArray earthquakes = json.getJSONArray("earthquakes"); 

請幫助如何獲取這種類型的JSON響應的。提前致謝。

回答

2

雅它是非常複雜的,但我是指你使用GSON庫來解析JSON,因爲它是在結構的方式解析example

+0

成功完成謝謝。 – rajesh

0
 private String jString = "{\"menu\": {\"id\": \"file\", \"value\": \"File\", \"popup\": { \"menuitem\": [ {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"}, {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"}, {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}]}}}"; 

     JSONObject menuObject = jObject.getJSONObject("menu"); 
     String attributeId = menuObject.getString("id"); 
     String attributeValue = menuObject.getString("value"); 
     JSONObject popupObject = menuObject.getJSONObject("popup"); 
     JSONArray menuitemArray = popupObject.getJSONArray("menuitem"); 

隨着每個新的曲線花括號都調用getJSONObject和子調用getString。

或者你可以按照Gson的概念來獲取響應。

0

完成與

JSONObject menuObject = jObject.getJSONObject("menu"); 
    String attributeValue = menuObject.getString("value");