gpt4 book ai didi

java - 解析从 Google 距离矩阵 API 发送的 Json 响应

转载 作者:行者123 更新时间:2023-12-02 12:10:03 50 4
gpt4 key购买 nike

当目的地或出发地位于英国境外时,会出现此 json,因此不会给我任何结果!我需要对此进行空检查,这样我就不会收到空指针异常

JSON 结果:

{
"destination_addresses" : [ "Durham, NC, USA" ],
"origin_addresses" : [ "Lancashire, UK" ],
"rows" : [
{
"elements" : [
{
"status" : "ZERO_RESULTS"
}
]
}
],

“状态”:“正常” }

代码:

public static void extractJsonFromRequest(GoogleResponsePojo response) {
String destinationAddress =
response.getDestination_addresses().get(0);
String timeTaken =
response.getRows().get(0).getElements().get(0).getDuration().getText();
String originAddress = response.getOrigin_addresses().get(0);

System.out.println("It will take ** "+ timeTaken + " ** to walk
from " + originAddress + " to " + destinationAddress);
}

Google Reponse POJO,它是 json 的结构:

public class GoogleResponsePojo {

private List<String> destination_addresses;
private List<String> origin_addresses;
private List<Rows> rows;

public List<String> getDestination_addresses() {
return destination_addresses;
}

public void setDestination_addresses(List<String> destination_addresses) {
this.destination_addresses = destination_addresses;
}

public List<String> getOrigin_addresses() {
return origin_addresses;
}

public void setOrigin_addresses(List<String> origin_addresses) {
this.origin_addresses = origin_addresses;
}

public List<Rows> getRows() {
return rows;
}

public void setRows(List<Rows> rows) {
this.rows = rows;
}
}

class Rows {
private List<Element> elements;

public List<Element> getElements() {
return elements;
}

public void setElements(List<Element> elements) {
this.elements = elements;
}
}

class Element {
private TextValue distance;
private TextValue duration;

public TextValue getDistance() {
return distance;
}

public void setDistance(TextValue distance) {
this.distance = distance;
}

public TextValue getDuration() {
return duration;
}

public void setDuration(TextValue duration) {
this.duration = duration;
}
}

class TextValue {
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;
}
}

我本质上只需要解析下面的 json,这样我就可以说(如果 status != ZERO_RESULTS )保存响应,否则抛出异常!可能很容易,但我很挣扎!非常感谢!

最佳答案

将 GoogleResponsePojo 更改为:

 public class GoogleResponsePojo {

private List<String> destination_addresses;
private List<String> origin_addresses;
private List<Rows> rows;

public void setDestination_addresses(List<String> destination_addresses) {
this.destination_addresses = destination_addresses;
}

public void setOrigin_addresses(List<String> origin_addresses) {
this.origin_addresses = origin_addresses;
}

public void setRows(List<Rows> rows) {
this.rows = rows;
}

//getters
}


private static class Rows{
private List<Element> elements;

public void setElements(List<Element> elements) {
this.elements = elements;
}

//getters
}

private static class Element{
private TextValue distance;
private TextValue duration;

public void setDistance(TextValue distance) {
this.distance = distance;
}

public void setDuration(TextValue duration) {
this.duration = duration;
}

//getters
}

private static class TextValue{
private String text;
private String value;

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

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

//getters
}

您可以按如下方式获取持续时间:

GoogleResponsePojo name = gson.fromJson(response.toString(),GoogleResponsePojo.class);

name.getRows().get(0).getDuration().getText();

我建议使用http-request基于 Apache http API 构建。使用简单:

 public static final String BASE_URL = "https://maps.googleapis.com/maps/api/distancematrix/json";

private static final HttpRequest<GoogleResponsePojo> HTTP_REQUEST =
HttpRequestBuilder.createGet(BASE_URL, GoogleResponsePojo.class)
.addDefaultRequestParameter("origins", "Seattle")
.addDefaultRequestParameter("destinations", "San+Francisco")
.addDefaultRequestParameter("key", "***")
.build();

@GET
@Produces(MediaType.APPLICATION_JSON)
public static void updateTestExecutionDetails() throws IOException {

ResponseHandler<GoogleResponsePojo> responseHandler = HTTP_REQUEST.execute();

GoogleResponsePojo name = responseHandler.orElseThrow(); // throws ResponseException when status code != 200

System.out.println(name.getDestination_addresses().get(0));
System.out.println(name.getRows().get(0).getElements().get(0).getDuration().getText());
System.out.println(name.getOrigin_addresses().get(0));
}

关于java - 解析从 Google 距离矩阵 API 发送的 Json 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46610287/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com