java - 如何从一个非常大的 JSON 数组中获取数据?

标签 java android arrays json

我正在为游戏使用 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;
        }
    }

This是 Json 对象和 logcat 警告的结构,示例 jsonobject 是 here

最佳答案

试试这个..

{                // 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/

相关文章:

java - 有什么方法可以将字符串数组转换为字符数组吗?

java - 如何在 GWT 中加入 Google Adsense

java - 如何在 Java 中抓取 scholar.google.com?

java - java xml字符串之间的子字符串提取

android - 在 android 中打开软键盘时不显示编辑文本

检查数组的排序

c# - 使用 foreach 循环遍历列表(或数组)作为对序列

java - 如何在 ISO8583 消息请求中设置子元素

android - 已更新至 Android Marshmallow——现在,Android Studio 无法定位设备

安卓在线动态数据库