2017-01-19 27 views
1

Ich möchte die Liste der Football-Spieler über API abrufen, habe ich den Http-Handler und API-Aufruf richtig gemacht. Jetzt habe ich den JSON-ArrayParsing JSON Array und Objekte

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

ich mag es analysieren, so dass nur der Name der Spieler gezeigt wird. Wie kann ich das erste Bit des JSON-Arrays analysieren, so dass das Array von [{Name: Paul Pogba ... bitte?

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

Mein Code so weit:

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

Veröffentlichen Sie den Code, den Sie zusammen mit dem Problem damit versucht haben. –

+0

versuchen Sie, JSON-Parser-Bibliotheken wie Gson oder Jackson zu verwenden. – Yoleth

Antwort

0

Danke für die Info Jungs. Fixed it with substring :)