2014-03-25 72 views
3

我對此有點困惑...無法解析Android中的JSonArray

我有一個PHP腳本,它將以下Json字符串返回給Android應用程序。

{"result":"OK","teams":[{"id":"1","tname":"AAA","tcity":"Paris","tfy":"1901"},{"id":"2","tname":"BBB","tcity":"Barcelona","tfy":"1901"}]} 

的問題是,由於某種原因,我無法讀取由「團隊」使用下面的Android的代碼標識的JSonArray:

@Override 
    protected void onPostExecute(String result) 
    { 
     dialog.dismiss(); 

     try { 
      JSONArray teams = null; 
      JSONObject json_response = new JSONObject(result); 

      if(json_response.get("result").toString().compareTo("ERR")==0) { 
       AlertDialog.Builder adig = new AlertDialog.Builder(ListTeamsActivity.this); 
       adig.setMessage(json_response.get("status").toString()); 
       adig.setCancelable(true); 
       AlertDialog diag = adig.create(); 
       diag.show(); 
      } else { 
       // process the answer 
       Log.d("debug_msg", "Teams = " + json_response.get("teams").toString()); 

       teams = new JSONArray(json_response.getJSONArray("teams")); 

       for(int n=0; n< teams.length(); n++) { 
        JSONObject t = teams.getJSONObject(n); 
        arrTeams[n] = t.getString("tname"); 
        Log.d("debug_msg", "Team = " + t.getString("tname")); 
       } 

       ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.activity_list_item, arrTeams); 
       lv.setAdapter(adapter); 
      } 

     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 
} 

這總是導致同樣的錯誤:

org.json.JSONException: Not a primitive array: class org.json.JSONArray 

以下是完整的輸出:

03-25 14:15:19.577: D/debug_msg(1960): Response = {"result":"OK","teams":[{"id":"1","tname":"Sport Lisboa e Benfica","tcity":"Lisboa","tfy":"1901"},{"id":"2","tname":"Sport Lisboa e Benfica","tcity":"Lisboa","tfy":"1901"}]} 
03-25 14:15:19.647: D/debug_msg(1960): Teams = [{"tname":"Sport Lisboa e Benfica","id":"1","tfy":"1901","tcity":"Lisboa"},{"tname":"Sport Lisboa e Benfica","id":"2","tfy":"1901","tcity":"Lisboa"}] 
03-25 14:15:19.657: W/System.err(1960): org.json.JSONException: Not a primitive array: class org.json.JSONArray 
03-25 14:15:19.677: W/System.err(1960):  at org.json.JSONArray.<init>(JSONArray.java:116) 
03-25 14:15:19.687: W/System.err(1960):  at pt.iscte.daam.soccerleague.ListTeamsActivity$ListTeams.onPostExecute(ListTeamsActivity.java:113) 
03-25 14:15:19.697: W/System.err(1960):  at pt.iscte.daam.soccerleague.ListTeamsActivity$ListTeams.onPostExecute(ListTeamsActivity.java:1) 
03-25 14:15:19.697: W/System.err(1960):  at android.os.AsyncTask.finish(AsyncTask.java:632) 
03-25 14:15:19.697: W/System.err(1960):  at android.os.AsyncTask.access$600(AsyncTask.java:177) 
03-25 14:15:19.707: W/System.err(1960):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645) 
03-25 14:15:19.707: W/System.err(1960):  at android.os.Handler.dispatchMessage(Handler.java:102) 
03-25 14:15:19.707: W/System.err(1960):  at android.os.Looper.loop(Looper.java:137) 
03-25 14:15:19.707: W/System.err(1960):  at android.app.ActivityThread.main(ActivityThread.java:4998) 
03-25 14:15:19.707: W/System.err(1960):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-25 14:15:19.717: W/System.err(1960):  at java.lang.reflect.Method.invoke(Method.java:515) 
03-25 14:15:19.717: W/System.err(1960):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 
03-25 14:15:19.717: W/System.err(1960):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) 
03-25 14:15:19.727: W/System.err(1960):  at dalvik.system.NativeStart.main(Native Method) 

感謝您的幫助....

回答

7

爲什麼要創建一個新的JSON數組?你不需要創建一個新的,直接獲取它。

teams = json_response.getJSONArray("teams"); 
+1

謝謝,這有幫助!我還有另一個問題,我正在使用的字符串數組... – carlosserrao