2016-01-15 41 views
-1

我得到了一個JSON,我試圖根據值解析和排序分數。我甚至無法解析它。以下是我的JSON響應。如何在android中解析jsonArray?

[ 
     {"scores": 
     { "a":1.460211E-17, 
      "b":3.808661E-20, 
      "c":3.07329371E-14, 
      "d":1.02141569E-17, 
      "e":1, 
      "f":6.26432543E-12, 
      "g":3.75437664E-14, 
      "h":1.877707E-21 
     }, 
     "rectangle": 
     { "left":142, 
     "width":882, 
     "top":0, 
     "height":848 
     } 
    } 
    ] 

我試圖將字符串轉換爲JSONObject,但錯誤表示它是JSONArray。

JSONObject reader = new JSONObject(json); 
JSONArray jsonArray = reader.optJSONArray("scores"); 

org.json.JSONException: Value of type org.json.JSONArray cannot be converted to JSONObject 

得到錯誤,所以我認爲值已經是一個JSON陣列。我想下面的代碼...

JSONArray jsonArray = reader.optJSONArray("scores"); 

JSONArray cast = jsonResponse.getJSONArray("scores"); 

...但它引發以下錯誤:

cannot resolve method optJSONArray()java.lang.String) 
cannot resolve method getJSONArray()java.lang.String) 
+0

你爲什麼downvote?請告訴我,我可以改善我的問題? – undefined

+2

*類型org.json.JSONArray的值不能轉換爲JSONObject *問bazillion次...學習json基礎... *無法解決方法optJSONArray()java.lang.String)* <=明顯的字符串(或任何' jsonResponse'類型是)沒有'optJSONArray'方法... – Selvin

+0

http://www.tutorialspoint.com/android/android_json_parser.htm –

回答

1

你的錯誤org.json.JSONException: Value of type org.json.JSONArray cannot be converted to JSONObject清楚地說,你正在嘗試將JSONArray解析爲JSONObject

做到這一點

JSONArray reader = new JSONArray(json); 
JSONObject jsonArray = reader.getJSONObject(0).optJSONObject("scores"); // replace 0 with your own index 
+0

感謝您的回答。 編輯器在JsonArray中拋出類似'optJSONObject(int)的錯誤connot被應用於(java.lang.String)' – undefined

+0

哦,是的,我錯過了一些東西。請現在嘗試。 – Rohit5k2

+0

它的工作感謝夥計 – undefined

0

試試這個如果,例如,你從一個HTTP請求獲取的JSON:

HttpPost httppost = new HttpPost("http://www.somepage.php"); 

HttpParams mHttpParams = new BasicHttpParams(); 
HttpConnectionParams.setConnectionTimeout(mHttpParams, myConnectionTimeOut); 
HttpConnectionParams.setSoTimeout(mHttpParams, myTimeOut); 

HttpClient httpclient = new DefaultHttpClient(mHttpParams); 
HttpResponse response = httpclient.execute(httppost); 
HttpEntity entity = response.getEntity(); 
BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent())); 
StringBuilder sb = new StringBuilder(); 

String line = null; 
while ((line = rd.readLine()) != null) { 
    sb.append(line ); 
} 

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

for(int i = 0; i < jsonArray .length(); i++) { 
    /* Do what you want here*/               
} 

希望它可以幫助

0
try { 
     JSONArray array= new JSONArray("your string"); 

     JSONObject object= array.getJSONObject(0); 
     JSONObject 
     scores=object.getJSONObject("scores"); 
     String [] subjects={"a","b","c","d","e","f","g","h"}; 
     for(int i=0;i<subjects.length;i++){ 

      Log.e("TAG",scores.getString(subjects[i])); 

     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
+0

是的,它的工作原理。我怎麼能讓它成爲循環而不是得到a,b,c? – undefined

+0

我已經更新了答案,看看它是否有幫助。因爲它們是json對象中的鍵,所以只能將鍵放入數組中,並從循環中獲取json對象中的所有值。 – Krishna