java - 解析 JSON 数组和对象

标签 java android

我想通过 API 检索足球运动员列表,我已正确进行 Http 处理程序和 api 调用。现在我有了这个 JSON 数组

http://api.football-data.org/v1/teams/66/players

我想对其进行解析,以便仅显示玩家的姓名。我如何解析 JSON 数组的第一位,以便数组从 [{name:Paul Pogba... 开始?

First bit of the JSON array that i want to get through

到目前为止我的代码:

@Override
    protected Void doInBackground(Void... arg0) {
        //New instance of http
        http sh = new http();

        // Making a request to URL and getting response
        final String jsonStr = sh.makeServiceCall(url);

        Log.e(TAG, "Response from: " + jsonStr);

        if (jsonStr != null) {
            try {
                // Getting JSON Array node
                JSONArray jsonarray = new JSONArray(jsonStr);


                for (int i = 0; i < jsonarray.length(); i++) {
                    JSONObject jo = jsonarray.getJSONObject(i);

                    String name = jo.getString("name");

                    HashMap<String, String> player = new HashMap<>();

                    player.put("name", name);

                    playerlist.add(player);
                }
            } catch (final JSONException e) { //In case an error regarding JSON parsing takes place
                Log.e(TAG, "Json parsing error: " + e.getMessage());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Json parsing error: " + e.getMessage(),
                                Toast.LENGTH_LONG)
                                .show();
                    }
                });

            }
        } else {
            Log.e(TAG, "Couldn't get json from server."); //In case the JSON can't be obtained from the server
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Couldn't get JSON from server. Check LogCat for possible errors!",
                            Toast.LENGTH_LONG)
                            .show();
                }
            });

        }

        return null;
    }

最佳答案

谢谢各位提供的信息。用子字符串修复了它:)

关于java - 解析 JSON 数组和对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41745568/

相关文章:

java - 使用 log4j2 配置文件设置记录器时

java - 如何将 SQL 表转换为 Redis 数据

android - 使用 "Parameters for notification messaging by platform GCM"时发送参数 Activity (android)

java - Android Studio - 带选项卡布局的滑动 View

java - 在 hadoop 单节点集群中运行 mahout 20newsgroups 时出错

java - 备份/恢复共享首选项android

android - ListView 的内容占用大量内存

android - 具有多种 View 类型的 CursorRecyclerViewAdapter

java - 找不到参数的方法 abiFilters() [x86]

java - 是否有命令行工具告诉我特定类在哪个 JAR 中