如何使用 GSON 获取 JSON 数据

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


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


爪哇代码:


    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如果有帮助的话,相当于我正在尝试的内容。


提前致谢!


慕慕森
浏览 326回答 3
3回答

一只萌萌小番薯

JsonObject jObj=(JsonObject)albums.get("students").getAsJsonArray().get(0); System.out.println(jObj.get("LastChapelAttended"));将其作为 JsonArray 获取,然后遍历该数组以获取 LastChapelAttended。

不负相思意

首先,students不是对象而是数组。因此students.LastChapelAttended不应该在任何语言中工作。如果你想检索LastChapelAttended数组中的第一个学生students[0].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"));    }}

慕姐4208626

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");}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java