我对此感到有些困惑......
我有一个PHP脚本,它将以下Json字符串返回给Android应用程序。
{"result":"OK","teams":[{"id":"1","tname":"AAA","tcity":"Paris","tfy":"1901"},{"id":"2","tname":"BBB","tcity":"Barcelona","tfy":"1901"}]}
问题是,出于某种原因,我无法阅读由"团队"所识别的JSonArray。使用以下Android代码:
@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)
感谢您的帮助......
答案 0 :(得分:7)
为什么要创建新的JSON数组?您不需要创建一个新的,只需直接获取它。
teams = json_response.getJSONArray("teams");