访问要存储在 AWS Dynamo 数据库表中的嵌套 JSON 对象

我希望访问休息请求中的嵌套 JSON 对象,以将其存储为 AWS 上的 Dynamo DB 表中的主键。


Json 对象的格式如下:


{

 "api": {

    "results": 543,

    "fixtures": [

      {

        "fixture_id": 95095,

        "venue": "Estádio Rei Pelé",

        "goalsHomeTeam": 1,

        "goalsAwayTeam": 1,

        "awayTeam": {

          "logo": "https://media.api-football.com/teams/156.png",

          "team_id": 156,

          "team_name": "Sao Bento"

        },

        "event_timestamp": 1569715200,

        "referee": null,

        "elapsed": 90,

        "score": {

          "halftime": "0-0",

          "penalty": null,

          "fulltime": "1-1",

          "extratime": null

        },

        "round": "Regular Season - 25",

        "event_date": "2019-09-29T00:00:00+00:00",

        "statusShort": "FT",

        "homeTeam": {

          "logo": "https://media.api-football.com/teams/146.png",

          "team_id": 146,

          "team_name": "CRB"

        },

我正在尝试使用以下代码访问它,但我NullPointerException在第 38 行上得到了一条信息,即以下开始的行int fixture_id。


我的代码如下:


JsonParser parser = new JsonFactory().createParser(new File("fixturesTwo.json"));


    JsonNode rootNode = new ObjectMapper().readTree(parser);

    Iterator<JsonNode> iter = rootNode.iterator();


    ObjectNode currentNode;



    while (iter.hasNext()) {

        currentNode = (ObjectNode) iter.next();


        int fixture_id = currentNode.path("api").findPath("fixtures").findValue("fixture_id").asInt();

        int league_id = currentNode.path("api").findPath("fixtures").findValue("league_id").asInt();


我猜这与我正在做的事情类似,我只是不知道我做错了什么。


红糖糍粑
浏览 81回答 1
1回答

料青山看我应如是

rootNode 有节点“API”。currentNode:{&nbsp; &nbsp;"results":543,&nbsp; &nbsp;"fixtures":[&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"fixture_id":95095,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"venue":"Estádio Rei Pelé",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"goalsHomeTeam":1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"goalsAwayTeam":1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"awayTeam":{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "logo":"https://media.api-football.com/teams/156.png",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "team_id":156,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "team_name":"Sao Bento"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"event_timestamp":1569715200,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"referee":null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"elapsed":90,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"score":{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "halftime":"0-0",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "penalty":null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "fulltime":"1-1",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "extratime":null&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"round":"Regular Season - 25",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"event_date":"2019-09-29T00:00:00+00:00",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"statusShort":"FT",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"homeTeam":{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "logo":"https://media.api-football.com/teams/146.png",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "team_id":146,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "team_name":"CRB"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;]}正确的代码:int fixture_id = currentNode.path("fixtures").findValue("fixture_id").asInt();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java