java 尝试在 Json 中的某个元素中获取元素或自身的值返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4425263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:18:19  来源:igfitidea点击:

Trying to get the value of an element or itself inside some element in Json returns null

javajsonelementgson

提问by Muaz

Hello Im trying to get the value of an element from a JsonObject. i'm using java with the Gson library.

您好,我正在尝试从 JsonObject 中获取元素的值。我在 Gson 库中使用 java。

here is the related part of the code:

这是代码的相关部分:

String s = "http://maps.googleapis.com/maps/api/directions/json?origin=30.065634,31.209473&destination=29.984177,31.440052&sensor=false";
URL url = new URL(s);

BufferedReader r = new BufferedReader(new InputStreamReader(
    ((HttpURLConnection) url.openConnection()).getInputStream()));

JsonStreamParser jsp = new JsonStreamParser(r);

JsonParser jp = new JsonParser();

JsonElement jsonElement =  jp.parse(r);

JsonObject jsonObject = jsonElement.getAsJsonObject();

System.out.println(jsonObject.get("summary"));

here is a part of the Json:

这是 Json 的一部分:

"status": "OK",   "routes": [ {
    "summary": "Mehwar Al Moneeb",
    "legs": [ {
      "steps": [ {
        "travel_mode": "DRIVING",
        "start_location": {
          "lat": 30.0655100,
          "lng": 31.2096600
        },
        "end_location": {
          "lat": 30.0654400,
          "lng": 31.2096000
        },
        "polyline": {
          "points": "mdovDksn}DLJ",
          "levels": "BB"
        },
        "duration": {
          "value": 1,
          "text": "1 min"
        },
        "html_instructions": "Head \u003cb\u003esouthwest\u003c/b\u003e on \u003cb\u003eOmar Toson\u003c/b\u003e toward \u003cb\u003eMohammed Roshdi\u003c/b\u003e",
        "distance": {
          "value": 9,
          "text": "9 m"
        }
      }

When I get the top element "routes" its fine and printing it displays what inside the element but when I try to for example to get the element "summary" as shown, the return is null, thats whats is displayed in the output, so subsequently I cannot get the value of it. Whats wrong? how can get the value of any JsonElement? Please reply back to me as soon as you can. Thanks in advance.

当我得到顶部元素“路由”它很好并打印它显示元素内部的内容但是当我尝试例如获取元素“摘要”时,返回为空,这就是输出中显示的内容,所以随后我无法获得它的价值。怎么了?如何获取任何 JsonElement 的值?请尽快回复我。提前致谢。

采纳答案by Shane Daniel

Routes is a javascript array. Summary is a property of the first object in that array, so you need to use jsonObject.getAsJsonArray("routes").get(0).get("summary");or equivalent. I haven't worked with your Java library, but the JS code would be jsonObject.routes[0].summary

Routes 是一个 javascript 数组。摘要是该数组中第一个对象的属性,因此您需要使用jsonObject.getAsJsonArray("routes").get(0).get("summary");或等效。我没有使用过你的 Java 库,但 JS 代码是jsonObject.routes[0].summary

Edit: Looked up GSON Documentationand updated code.

编辑:查找GSON 文档和更新的代码。