繁体   English   中英

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

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

我需要在Java对象中解析Json并保存以下字段:destination_addresses,origin_addresses和duration。 我无法获得“持续时间”的值。 这是我必须解析的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 + '}';
        }


    }

执行此代码,我得到以下输出:

目的地:Blocco Palma Primo,95121 Fattoria Sole Delfino CT,意大利

来源:Contrada Torre Allegra,95121卡塔尼亚CT,意大利

持续时间{text = null,value = 0}

显然,我可以成功地解析destination_addresses和origin_addresses字段,但是不能解析持续时间,因为持续时间为0和null。 我哪里错了? 如何解决此问题并获得正确的持续时间值(文本和值)? 谢谢您帮忙。

在此,在Java中,类名应以大写字母开头。

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

我没有对持续时间和距离对象进行单独的分类,因为它们都具有相同的type和name属性。

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 + '}';
    }


}

暂无
暂无

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

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