简体   繁体   English

获取嵌套的 JSONObject Java?

[英]Get Nested JSONObject Java?

I am trying to get a JSON object nested inside another JSON object.我正在尝试将 JSON 对象嵌套在另一个 JSON 对象中。 When I run my main class:当我运行我的主类时:

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");
}
}

I get the error:我收到错误:

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

  JSONObject["standard"] is not a JSONObject.

I am using the the maven artifact org.json:json package.我正在使用maven artifact org.json:json包。

This is a part of json that returns url in the example这是示例中返回 url 的 json 的一部分

  {
  "_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"
          }
        ],

as you can see standard is not an object.正如你所看到的,标准不是一个对象。 It is array.是数组。

You should change your code as follows你应该改变你的代码如下

JSONArray standard =League.getJSONArray("standard");
    for (int i = 0; i < standard.length(); i++) {
        String firstName = standard.getJSONObject(i).getString("firstName");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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