gson从google matrix api解析嵌套的json对象

时间:2013-12-05 17:27:57

标签: java json gson

我想发布这样的帖子here,但是使用这个JSON响应:

{
  "status": "OK",
  "origin_addresses": [ "Vancouver, BC, Canada", "Seattle, État de Washington, États-Unis" ],
  "destination_addresses": [ "San Francisco, Californie, États-Unis", "Victoria, BC, Canada" ],
  "rows": [ {
    "elements": [ {
      "status": "OK",
      "duration": {
        "value": 340110,
        "text": "3 jours 22 heures"
      },
      "distance": {
        "value": 1734542,
        "text": "1 735 km"
      }
    }, {
      "status": "OK",
      "duration": {
        "value": 24487,
        "text": "6 heures 48 minutes"
      },
      "distance": {
        "value": 129324,
        "text": "129 km"
      }
    } ]
  }, {
    "elements": [ {
      "status": "OK",
      "duration": {
        "value": 288834,
        "text": "3 jours 8 heures"
      },
      "distance": {
        "value": 1489604,
        "text": "1 490 km"
      }
    }, {
      "status": "OK",
      "duration": {
        "value": 14388,
        "text": "4 heures 0 minutes"
      },
      "distance": {
        "value": 135822,
        "text": "136 km"
      }
    } ]
  } ]
}

我的课程是:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;

class Response {
    private String status;
    private String[] destination_addresses;
    private String[] origin_addresses;
    private Elements[] rows;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String[] getDestination_addresses() {
        return destination_addresses;
    }

    public void setDestination_addresses(String[] destination_addresses) {
        this.destination_addresses = destination_addresses;
    }

    public String[] getOrigin_addresses() {
        return origin_addresses;
    }

    public void setOrigin_addresses(String[] origin_addresses) {
        this.origin_addresses = origin_addresses;
    }

    public Elements[] getRows() {
        return rows;
    }

    public void setRows(Elements[] rows) {
        this.rows = rows;
    }
}

class Distance {
    private String text;
    private String value;

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

class Duration {
    private String text;
    private String value;

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

class Elements {
    Duration duration[];
    Distance distance[];
    String status;
}

public class JSON {
    public static void main(String[] args) throws IOException {
        JsonReader reader = new JsonReader(new BufferedReader(new FileReader(
                "json.json")));
        reader.setLenient(true);
        Response r = (new Gson().fromJson(reader, Response.class));
        StringBuilder sb = new StringBuilder();
        for (String s : r.getDestination_addresses()) {
            sb.append(s);
        }
        System.out.println("getDestination_addresses: " + sb.toString());
        StringBuilder sb1 = new StringBuilder();
        for (String s : r.getOrigin_addresses()) {
            sb1.append(s);
        }
        System.out.println("getOrigin_addresses: " + sb1.toString());
        System.out.println("getStatus(): " + r.getStatus());
        System.out.println("Rows length " + r.getRows().length);
        System.out.println(r.getRows()[0].status);   // here i get null
    }
}

但它没有完全发挥作用,我只能正确地获得这些字段:

private String status;
private String[] destination_addresses;
private String[] origin_addresses;

信息为空。

1 个答案:

答案 0 :(得分:1)

你的声明是错误的。将Response更改为

class Response {
    private String status;
    private String[] destination_addresses;
    private String[] origin_addresses;
    private Item[] rows;
    ...
}

其中Item是:

 class Item {
    private Element[] elements;
    ...
 }

Element是:

 class Element{
    Duration duration;
    Distance distance;
    String status;
    ...
 }

这应该解决。还有三个提示:

  1. 我们处于完整的仿制时代,所以要避免使用Element []并使用List(依此类推,无论如何,我都会让你“风格”回答)
  2. 使用类似this的内容来显示您的JSON,它将帮助您了解其结构
  3. DurationDistance具有相同的结构,也许你可以保存一个声明,Gson不关心类的名称,它看它的结构。从Gson的角度来看,持续时间和距离是相同的:一个字符串加一个整数。