我正在为游戏使用 API,它返回一个非常大的 JSON 对象。问题是当我尝试访问其中的整数时,它不起作用并在 logcat 中显示 JSON 对象的一部分,并带有橙色文本 system.err。我怎样才能访问这个数组中我想要的任何值?
json对象的代码和结构如下所示。
private class getGame extends AsyncTask<Integer, Integer, String>{
Context context;
private getGame(Context context) {
this.context = context.getApplicationContext();
}
String response;
@Override
protected String doInBackground(Integer... params) {
HttpClient client = new DefaultHttpClient();
String geturl = "https://eu.api.pvp.net/api/lol/tr/v1.3/game/by-summoner/1795120/recent?api_key=cea2196c-6a12-474f-8f6d-52ad5e612cbc";
HttpGet get = new HttpGet(geturl);
HttpResponse responseGet = null;
try {
responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
response = EntityUtils.toString(resEntityGet);
JSONObject jsonObj = new JSONObject(response);
JSONObject gamesObj = jsonObj.getJSONObject("games");
JSONObject game0 = jsonObj.getJSONObject("0");
Log.e("test", ""+game0.getInt("gameId"));
return "";
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
最佳答案
试试这个..
{ // JSONObject
"games": [ // JSONArray
改变这个
JSONObject gamesObj = jsonObj.getJSONObject("games");
至
JSONArray gamesObj = jsonObj.getJSONArray("games").getJSONObject(0).getJSONArray("fellowPlayers");
JSONObject game0 = jsonObj.getJSONObject(0);
更多说明
JSONObject jsonObj = new JSONObject(response);
JSONArray gamesarray = jsonObj.getJSONArray("games");
JSONObject game0 = gamesarray.getJSONObject(0);
JSONArray fellowarray = game0.getJSONArray("fellowPlayers");
JSONObject fellow0 = fellowarray.getJSONObject(0);
Log.e("test", ""+fellow0.getInt("gameId"));
关于java - 如何从一个非常大的 JSON 数组中获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23929296/