2017-12-10 15 views
0

別のJSONオブジェクト内に入れ子になったJSONオブジェクトを取得しようとしています。私は私のメインクラスを実行すると:入れ子になったJSONObject Javaを取得しますか?

import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.IOException; 

public class Main { 
public static void main(String[] args) throws IOException, JSONException { 
    NBAPlayers players = new NBAPlayers(); 
    JSONObject json = players.readJsonFromUrl("http://data.nba.net/10s/prod/v1/2017/players.json"); 
    JSONObject League = json.getJSONObject("league"); 
    JSONObject standard =League.getJSONObject("standard"); 
    JSONObject firstName = standard.getJSONObject("firstName"); 



} 

}

を私はエラーを取得する:JSON:

Exception in thread "main" org.json.JSONException: 

    JSONObject["standard"] is not a JSONObject. 

私はMavenのアーティファクトorg.jsonを使用しています。それが役立つだろうならば、私は私の他のクラス

答えて

0

を供給することができますこれは、標準のオブジェクトではありません見ることができるように例では

{ 
    "_internal": { 
    "pubDateTime": "2017-12-10 11:42:23.504", 
    "xslt": "xsl/league/roster/marty_active_players.xsl", 
    "eventName": "league_roster" 
    }, 
    "league": { 
    "standard": [ 
     { 
     "firstName": "Alex", 
     "lastName": "Abrines", 
     "personId": "203518", 
     "teamId": "1610612760", 
     "jersey": "8", 
     "isActive": true, 
     "pos": "G", 
     "heightFeet": "6", 
     "heightInches": "6", 
     "heightMeters": "1.98", 
     "weightPounds": "190", 
     "weightKilograms": "86.2", 
     "dateOfBirthUTC": "1993-08-01", 
     "teams": [ 
      { 
      "teamId": "1610612760", 
      "seasonStart": "2016", 
      "seasonEnd": "2017" 
      } 
     ], 
     "draft": { 
      "teamId": "1610612760", 
      "pickNum": "32", 
      "roundNum": "2", 
      "seasonYear": "2013" 
     }, 
     "nbaDebutYear": "2016", 
     "yearsPro": "1", 
     "collegeName": "", 
     "lastAffiliation": "Spain/Spain", 
     "country": "Spain" 
     }, 
     { 
     "firstName": "Quincy", 
     "lastName": "Acy", 
     "personId": "203112", 
     "teamId": "1610612751", 
     "jersey": "13", 
     "isActive": true, 
     "pos": "F", 
     "heightFeet": "6", 
     "heightInches": "7", 
     "heightMeters": "2.01", 
     "weightPounds": "240", 
     "weightKilograms": "108.9", 
     "dateOfBirthUTC": "1990-10-06", 
     "teams": [ 
      { 
      "teamId": "1610612761", 
      "seasonStart": "2012", 
      "seasonEnd": "2013" 
      }, 
      { 
      "teamId": "1610612758", 
      "seasonStart": "2013", 
      "seasonEnd": "2013" 
      }, 
      { 
      "teamId": "1610612752", 
      "seasonStart": "2014", 
      "seasonEnd": "2014" 
      }, 
      { 
      "teamId": "1610612758", 
      "seasonStart": "2015", 
      "seasonEnd": "2015" 
      }, 
      { 
      "teamId": "1610612742", 
      "seasonStart": "2016", 
      "seasonEnd": "2016" 
      }, 
      { 
      "teamId": "1610612751", 
      "seasonStart": "2016", 
      "seasonEnd": "2017" 
      } 
     ], 

をURLを返すJSONの一部です。それは配列です。

JSONArray standard =League.getJSONArray("standard"); 
    for (int i = 0; i < standard.length(); i++) { 
     String firstName = standard.getJSONObject(i).getString("firstName"); 
    } 
を次のようにあなたがあなたのコードを変更する必要があります
関連する問題