简体   繁体   English

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

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

I need to parsing Json in Java object and save the fields: destination_addresses, origin_addresses and duration. 我需要在Java对象中解析Json并保存以下字段:destination_addresses,origin_addresses和duration。 I can not get the values of "duration". 我无法获得“持续时间”的值。 This is the json which I have to parse: 这是我必须解析的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"
}

And this is what I tried in Java, to get the fields that I need: 这就是我在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());



    }

With these classes: 使用这些类:

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


    }

Executing this code I get the following output: 执行此代码,我得到以下输出:

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

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

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

Obviously, I can successfully do the parsing of fields destination_addresses and origin_addresses, but not the duration, of which gives me values 0 and null. 显然,我可以成功地解析destination_addresses和origin_addresses字段,但是不能解析持续时间,因为持续时间为0和null。 Where am I wrong? 我哪里错了? How can I solve this problem and get the correct values (text and value) of duration? 如何解决此问题并获得正确的持续时间值(文本和值)? Thank for your help. 谢谢您帮忙。

Here, In java class names should start with uppercase. 在此,在Java中,类名应以大写字母开头。

If it is problem to access objects just define getter methods. 如果访问对象有问题,只需定义getter方法。

I didn't make seperate classes of duration and distance objects because of that they are both have same type and name properties. 我没有对持续时间和距离对象进行单独的分类,因为它们都具有相同的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