简体   繁体   中英

Parsing JSON Array and Objects

I want to retrieve the list of football players via API, I have made the Http handler and api call correctly. Now i have this JSON array

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

I want to parse it so that only the name of the players is shown. How can i parse through the first bit of the JSON array so that the array starts from [{name:Paul Pogba... please?

我想要通过的JSON数组的第一位

My code so far:

@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;
    }

thanks for the info guys. Fixed it with substring :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM