尝试获取 Json 中某个元素内的元素或其本身的值返回 null

发布于 2024-10-07 10:27:59 字数 1553 浏览 0 评论 0原文

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

这是代码的相关部分:

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"));

这是 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"
        }
      }

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

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

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:

"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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

佞臣 2024-10-14 10:27:59

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

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

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

Edit: Looked up GSON Documentation and updated code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文