【问题标题】:How to get JSON data using GSON如何使用 GSON 获取 JSON 数据
【发布时间】:2019-02-12 10:44:27
【问题描述】:

这可能以前被问过,但我不知道我的问题的术语,因此不知道要查找什么。

我正在使用 GSON 和 Java 尝试从解析的 JSONElement 中获取信息。

Java 代码:

    JsonParser parser = new JsonParser();

    String url = "https://chapel-logs.herokuapp.com/attendance";

    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    // optional default is GET
    con.setRequestMethod("GET");

    //add request header
    con.setRequestProperty("Accept", "application/json");

    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'GET' request to URL : " + url);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }

    in.close();

    //print result
    JsonElement element = parser.parse(response.toString());

    if (element.isJsonObject()) {
        JsonObject albums = element.getAsJsonObject();
        System.out.println(albums.get("students")); //prints out data in students
        System.out.println(albums.get("students.LastChapelAttended")); //error
    }

我的 JSON :

{"students":[{"LastChapelAttended":{"Loc":"","Chapel":"WhyChapel","Year":2018,"Month":9,"Day":6,"Hour":15,"Min":14,"Sec":28},"StudNum":"F02660934","Attendance":17},{"LastChapelAttended":{"Loc":"","Chapel":"WhyChapel","Year":2018,"Month":9,"Day":5,"Hour":19,"Min":49,"Sec":11},"StudNum":"002660934","Attendance":2},{"LastChapelAttended":{"Loc":"","Chapel":"WhyChapel","Year":2018,"Month":9,"Day":4,"Hour":20,"Min":35,"Sec":57},"StudNum":"002643472","Attendance":2},{"LastChapelAttended":{"Loc":"","Chapel":"WhyChapel","Year":2018,"Month":9,"Day":7,"Hour":5,"Min":34,"Sec":54},"StudNum":"002664906","Attendance":1}]}

我要获取的数据是:LastChapelAttended,但是LastChapelAttended 在students 内。在 JavaScript 中,如果有帮助,我正在尝试的就是 students.LastChapelAttended。

提前致谢!

【问题讨论】:

    标签: java json gson


    【解决方案1】:

    只需遍历学生数组

    albums.getAsJsonArray("students")
          .forEach(student -> System.out.println(student.getAsJsonObject()
                                .get("LastChapelAttended")));
    

    【讨论】:

      【解决方案2】:

      首先,students 不是一个对象,而是一个数组。因此students.LastChapelAttended 不应该以任何语言工作。如果您想检索数组students[0].LastChapelAttended 中第一个学生的LastChapelAttended 应该可以。

      我对gson不熟悉,但我认为你想做的是这样的:

      if (element.isJsonObject()) {
          JsonObject albums = element.getAsJsonObject();
          JsonArray students = albums.getAsJsonArray("students");
      
          for (JsonElement student : students) {
              System.out.println(albums.getAsJsonObject().get("LastChapelAttended"));
          }
      }
      

      【讨论】:

        【解决方案3】:
        JsonObject jObj=(JsonObject)albums.get("students").getAsJsonArray().get(0);
        System.out.println(jObj.get("LastChapelAttended"));
        

        将其作为 JsonArray 获取,然后遍历该数组以获取 LastChapelAttended。

        【讨论】:

          【解决方案4】:

          您可以查看此内容以获取帮助Difference between JSONObject and JSONArray

          Students 是一个 JsonArray LastChapelAttended 是一个 JSONObject 你可以通过调用来获取它

          json = (json data)
          JsonParser parser = new JsonParser();
          JsonObject rootObj = parser.parse(json).getAsJsonObject();
          JsonArray studentsArray = rootObj.getAsJsonArray("students");
          for (JsonElement stu : studentsArray) {
          JsonObject student = stu.getAsJsonObject();
          JsonObject LastChapelAttended = student.getAsJsonObject("LastChapelAttended");
          
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-09-29
            • 1970-01-01
            • 1970-01-01
            • 2014-01-04
            • 1970-01-01
            相关资源
            最近更新 更多