2013-04-28 138 views
0

我已經從URL輸出如下:爪哇JSON陣列

[ 「HANK-TPSSL4應用內」, 「acgeneral應用內」, 「acgeneral幅材」, 「achalf應用內」, 「acproduct應用內」 ]

我的Java代碼,從這個數組獲取每個對象:

BufferedReader in = new BufferedReader(new InputStreamReader((HttpURLConnection) alertHandleUrl.openConnection().getInputStream())); 

     String output;  
     while ((output = in.readLine()) != null) { 

      JSONArray array = new JSONArray(output); 

       for (int i =0; i < array.length(); i++) 
        JSONObject row = array.getJSONObject(i); 
        System.out.println(row); 
       } 

我正的錯誤是在線程

異常 「主要」 org.json.JSONException:JSONArray [0]不是JSONObject。 在org.json.JSONArray.getJSONObject(JSONArray.java:330) 在com.ebay.sherlock.calc.AutoConfigFunc.main(AutoConfigFunc.java:70)

有人能幫助我嗎?在此先感謝

+0

你輸出應該看起來像{」myarray「:[」abc「,」def「]}然後新的JSONObject(輸出)。 getJSONArray(「myarray」)....它應該這樣做。或者試着讓你的輸出看起來像{[「abc」,「def」]}和新的JSONArray(輸出) – 2013-04-28 03:28:08

+0

嗨,由於我的輸出格式爲[「abc」,「def」]格式,我從這個JSON數組中獲取每個對象?謝謝.. – user2325703 2013-04-28 03:48:30

回答

0

是的,此輸出是一個簡單的字符串數組而不是JSON對象。你需要修剪前後括號,然後用逗號分割字符串,「

+0

嗨,我的輸出已經被逗號分隔「,」。這是我的輸出:[「HANK-TPSSL4-app」,「acgeneral-app」,「acgeneral-web」,「achalf-app」,「acproduct-app」] – user2325703 2013-04-28 03:45:49

+0

@ user2325703是的,但它是字符串。你需要使用split()方法拆分它並將其轉換爲String []數組 – gurvinder372 2013-04-28 03:47:23

+0

感謝它工作:) – user2325703 2013-04-28 05:45:55