使用 GSON 从 DistanceMatrix Google Api 解析嵌套数组 Json 到 Java

Parsing nested array Json to Java, from DistanceMatrix Google Api with GSON

我需要解析 Java 对象中的 Json 并保存字段:destination_addresses、origin_addresses 和持续时间。我无法获得“持续时间”的值。这是我必须解析的 json:

{
   "destination_addresses" : [ "Blocco Palma Primo, 95121 Fattoria Sole Delfino CT, Italia" ],
   "origin_addresses" : [
      "Unnamed Road, 95121 Catania CT, Italia",
      "Blocco Palma Primo, 95121 Fattoria Sole Delfino CT, Italia",
      "Contrada Torre Allegra, 95121 Catania CT, Italia",
      "Contrada Pantano d'Arci, Catania CT, Italia",
      "Unnamed Road, 95121 Catania CT, Italia",
      "Via Cassia, 95121 Catania CT, Italia",
      "Contrada Pantano d'Arci, Catania CT, Italia",
      "Contrada Pantano d'Arci, Catania CT, Italia",
      "Contrada Pantano d'Arci, Catania CT, Italia",
      "Contrada Pantano d'Arci, Catania CT, Italia"
   ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "2,0 km",
                  "value" : 2037
               },
               "duration" : {
                  "text" : "4 min",
                  "value" : 266
               },
               "status" : "OK"
            }
         ]
      },
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "1 m",
                  "value" : 0
               },
               "duration" : {
                  "text" : "1 min",
                  "value" : 0
               },
               "status" : "OK"
            }
         ]
      },
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "3,8 km",
                  "value" : 3768
               },
               "duration" : {
                  "text" : "7 min",
                  "value" : 400
               },
               "status" : "OK"
            }
         ]
      },
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "5,3 km",
                  "value" : 5304
               },
               "duration" : {
                  "text" : "6 min",
                  "value" : 374
               },
               "status" : "OK"
            }
         ]
      },
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "8,2 km",
                  "value" : 8239
               },
               "duration" : {
                  "text" : "13 min",
                  "value" : 785
               },
               "status" : "OK"
            }
         ]
      },
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "11,5 km",
                  "value" : 11486
               },
               "duration" : {
                  "text" : "15 min",
                  "value" : 901
               },
               "status" : "OK"
            }
         ]
      },
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "12,2 km",
                  "value" : 12226
               },
               "duration" : {
                  "text" : "18 min",
                  "value" : 1099
               },
               "status" : "OK"
            }
         ]
      },
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "12,2 km",
                  "value" : 12226
               },
               "duration" : {
                  "text" : "18 min",
                  "value" : 1099
               },
               "status" : "OK"
            }
         ]
      },
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "12,2 km",
                  "value" : 12226
               },
               "duration" : {
                  "text" : "18 min",
                  "value" : 1099
               },
               "status" : "OK"
            }
         ]
      },
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "12,2 km",
                  "value" : 12226
               },
               "duration" : {
                  "text" : "18 min",
                  "value" : 1099
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

这就是我在 Java 中尝试获取所需字段的方法:

public static void main(String[] args) throws Exception {
        // TODO code application logic here
        URL url = new URL(myUrl);
        InputStreamReader reader = new InputStreamReader(url.openStream());
        SRD sr = new Gson().fromJson(reader, SRD.class);
        
        System.out.println("destination: " + sr.destination_addresses.get(0));
        System.out.println("origins: " + sr.origin_addresses.get(2));
        System.out.println(sr.rows.get(2).elements.get(0).toString());
        
         
        
    }

有了这些 类:

private class SRD {
        List<String> destination_addresses;
        List<String> origin_addresses;
        List<elements> rows;
        
    }
    
    
    private class elements {
        List<duration> elements;
    
    }
    
    private class duration {
        String text;
        int value;

        
        public String toString() {
            return "duration{" + "text=" + text + ", value=" + value + '}';
        }
        
        
    }

执行这段代码我得到以下输出:

destination: Blocco Palma Primo, 95121 Fattoria Sole Delfino CT, Italy

origins: Contrada Torre Allegra, 95121 Catania CT, Italy

duration{text=null, value=0}

显然,我可以成功解析字段 destination_addresses 和 origin_addresses,但不能成功解析持续时间,持续时间给我的值为 0 和 null。我哪里错了?我怎样才能解决这个问题并获得正确的持续时间值(文本和值)?感谢您的帮助。

这里,In java class 名字应该以大写开头。

如果访问对象有问题,只需定义 getter 方法。

我没有制作单独的 classes 持续时间和距离对象,因为它们都具有相同的类型和名称属性。

private class SRD {
    List<String> destination_addresses;
    List<String> origin_addresses;
    List<Row> rows;

}

private class Row{
    List<Element> elements;

}


private class Element {
    General duration;
    General distance;
    String status; 

}

private class General {
    String text;
    int value;


    public String toString() {
        return "duration{" + "text=" + text + ", value=" + value + '}';
    }


}