使用GSON将Distance嵌套数组Json解析为Java,来自DistanceMatrix Google Api

时间:2021-04-09 15:23:17

I need to parsing Json in Java object and save the fields: destination_addresses, origin_addresses and duration. I can not get the values of "duration". This is the json which I have to parse:

我需要在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"
}

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}

duration {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. Where am I wrong? How can I solve this problem and get the correct values (text and value) of duration? Thank for your help.

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

1 个解决方案

#1


0  

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.

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

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


}

#1


0  

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.

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

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


}