Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 访问嵌套JSON对象以存储在AWS Dynamo数据库表中_Java_Json_Amazon Web Services - Fatal编程技术网

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

Java 访问嵌套JSON对象以存储在AWS Dynamo数据库表中,java,json,amazon-web-services,Java,Json,Amazon Web Services,我希望通过rest请求访问一个嵌套的JSON对象,将其存储为AWS上Dynamo DB表中的主键 Json对象的格式如下: { "api": { "results": 543, "fixtures": [ { "fixture_id": 95095, "venue": "Estádio Rei Pelé", "goalsHomeTeam": 1, "goalsAwayTeam": 1,

我希望通过rest请求访问一个嵌套的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"
        },
我试图使用以下代码访问它,但在第38行,即起始
int fixture\u id
下面的一行,我得到了一个
NullPointerException

我的代码如下:

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();

        try {
            table.putItem(new Item().withPrimaryKey("fixture_id", fixture_id, "league_id", league_id)
                    .withJSON("Home Team", currentNode.path("api").findPath("fixtures").findPath("homeTeam").findValue("team_name").toString())
                    .withJSON("Away Team", currentNode.path("api").findPath("fixtures").findPath("awayTeam").findValue("team_name").toString()));
            System.out.println("PutItem succeeded: " + fixture_id + " " + league_id);

        } catch (Exception e) {
            System.err.println("Unable to add: " + fixture_id);
            System.err.println(e.getMessage());
            break;
        }
    }
    parser.close();

}
JsonParser parser=new JsonFactory().createParser(新文件(“fixturesTwo.json”);
JsonNode rootNode=newObjectMapper().readTree(解析器);
迭代器iter=rootNode.Iterator();
ObjectNode当前节点;
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();
试一试{
table.putItem(新项(),带主键(“fixture_id”,fixture_id,“league_id”,league_id)
.withJSON(“Home Team”,currentNode.path(“api”).findPath(“fixtures”).findPath(“homeTeam”).findValue(“团队名称”).toString()
.withJSON(“离开团队”,currentNode.path(“api”).findPath(“固定装置”).findPath(“awayTeam”).findValue(“团队名称”).toString());
System.out.println(“PutItem成功:+fixture\u id+”“+league\u id”);
}捕获(例外e){
System.err.println(“无法添加:“+fixture\u id”);
System.err.println(e.getMessage());
打破
}
}
parser.close();
}
我猜这与我正在做的事情类似,我只是不知道我做错了什么。

rootNode有节点“API”。currentNode:

{
   "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"
         }
      }
   ]
}
正确的代码:

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

可能的重复只会将第一个夹具id添加到表中。知道如何遍历并添加它们吗?啊,我看到我的迭代器指向“api”节点。我这么做就把它修好了<代码>迭代器iter=(rootNode.findPath(“fixtures”)).Iterator()