【问题标题】:Java JSONObject.getJSONArray always returns nullJava JSONObject.getJSONArray 总是返回 null
【发布时间】:2020-01-06 14:52:07
【问题描述】:

我想使用 Google 距离矩阵 API 来获取在两个位置之间旅行所需的持续时间。但是当我尝试从返回的数据(JSON 编码)中获取持续时间时,getJSONArray 方法总是返回 null。

这是 Google 发送的数据:

{
   "destination_addresses" : [ "Rome, Metropolitan City of Rome, Italy" ],
   "origin_addresses" : [ "Berlin, Germany" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "1,501 km",
                  "value" : 1501458
               },
               "duration" : {
                  "text" : "15 hours 5 mins",
                  "value" : 54291
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

这是获取持续时间的方法:

public static int getDurationFromJSON(String json){
    try {
        JSONObject jsonObj = new JSONObject(json)
                .getJSONArray("rows")
                .getJSONObject(0)
                .getJSONArray ("elements")
                .getJSONObject(0)
                .getJSONObject("duration");

        return (int)(jsonObj.getInt("value") / 60.0f + 0.5f);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return -1;
}

getJSONArray("rows") 返回 null。

【问题讨论】:

  • 您是否检查从 Google API 收到的数据。 json变量的内容是什么?
  • json变量的数据就是贴出来的json代码。它和我在浏览器中启动请求时得到的完全一样。
  • 但是你确定你在调试代码时将相同的内容放入变量“j​​son”吗?
  • 我 100% 确定。
  • 我建议您使用调试器查看 jsonObj 变量中的内容。如果函数返回 null 表示什么都没有,或者它不是预期的类型,如果你说你对内容很确定,那么你请求的类型是错误的。

标签: java android json nullpointerexception


【解决方案1】:

我不确定你为什么得到空值,但这条线似乎过分了:

(int)(Integer.parseInt(String.valueOf(jsonObj.getInt("value"))) / 60.0f + 0.5f);

JsonObj.getInt("Value) 将返回一个 int,你为什么要把它变成一个字符串,然后将它解析回一个 Int,然后再将它转换回一个 INT?

这可以简化成这样

 return(int)((jsonObj.getInt("value")/60.0f) +0.5f)

至于 null,我会使用调试器并检查传入的 JSON,并确保它是您认为的那样。

另外,正如其他人所建议的那样,使用 restTemplate 之类的东西将 json 自动解析为本机映射对象将使您的生活更轻松。

【讨论】:

  • 实际上,您提到的那一行是我将持续时间作为字符串的版本的剩余部分。现在它没有任何意义。
【解决方案2】:

好的,这里有解决方案。不要相信 org.json。* 使用 Gson:

Json 数据:

{
   "destination_addresses" : [ "Rome, Metropolitan City of Rome, Italy" ],
   "origin_addresses" : [ "Berlin, Germany" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "1,501 km",
                  "value" : 1501458
               },
               "duration" : {
                  "text" : "15 hours 5 mins",
                  "value" : 54291
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

为结果创建对象:

public class DirectionMatrixResult {
    private String[] destination_addresses;
    private String[] origin_addresses;

    private DirectionMatrixResultRow[] rows;

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

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

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

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

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

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

public class DirectionMatrixResultRow {
    private DirectionMatrixResultElement[] elements;

    public DirectionMatrixResultElement[] getElements() {
        return elements;
    }

    public void setElements(DirectionMatrixResultElement[] elements) {
        this.elements = elements;
    }
}

public class DirectionMatrixResultElement {
    private DirectionMatrixResultElementValue distance;
    private DirectionMatrixResultElementValue duration;
    private String status;

    public DirectionMatrixResultElementValue getDistance() {
        return distance;
    }

    public DirectionMatrixResultElementValue getDuration() {
        return duration;
    }

    public String getStatus() {
        return status;
    }

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

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

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

public class DirectionMatrixResultElementValue {
    private String text;
    private long value;

    public long getValue() {
        return value;
    }

    public String getText() {
        return text;
    }

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

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

然后调用:

public static int getDurationFromJSON(String json){
    try {
        Gson gson = new Gson();
        DirectionMatrixResult result = gson.fromJson(json, DirectionMatrixResult.class);
        return (int)(result.getRows()[0].getElements()[0].getDuration().getValue() / 60.0f + 0.0f);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return -1;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-29
    • 2020-04-09
    • 2010-11-05
    • 1970-01-01
    • 2014-03-04
    • 2016-11-02
    • 2014-05-06
    • 2012-06-05
    相关资源
    最近更新 更多