第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何使用 GSON 獲取 JSON 數(shù)據(jù)

如何使用 GSON 獲取 JSON 數(shù)據(jù)

慕慕森 2021-09-03 16:38:44
之前可能已經(jīng)問過這個(gè)問題,但我不知道我的問題的術(shù)語(yǔ),因此不知道要查找什么。我正在使用 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}]}我試圖獲得的數(shù)據(jù)是:LastChapelAttended,但是LastChapelAttended在students. 在 JavaScript 中,students.LastChapelAttended如果有幫助的話,相當(dāng)于我正在嘗試的內(nèi)容。提前致謝!
查看完整描述

3 回答

?
一只萌萌小番薯

TA貢獻(xiàn)1795條經(jīng)驗(yàn) 獲得超7個(gè)贊

JsonObject jObj=(JsonObject)albums.get("students").getAsJsonArray().get(0);
System.out.println(jObj.get("LastChapelAttended"));

將其作為 JsonArray 獲取,然后遍歷該數(shù)組以獲取 LastChapelAttended。


查看完整回答
反對(duì) 回復(fù) 2021-09-03
?
不負(fù)相思意

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊

首先,students不是對(duì)象而是數(shù)組。因此students.LastChapelAttended不應(yīng)該在任何語(yǔ)言中工作。如果你想檢索LastChapelAttended數(shù)組中的第一個(gè)學(xué)生students[0].LastChapelAttended應(yīng)該可以工作。


我對(duì)gson不熟悉,但我認(rèn)為你想做的是這樣的:


if (element.isJsonObject()) {

    JsonObject albums = element.getAsJsonObject();

    JsonArray students = albums.getAsJsonArray("students");


    for (JsonElement student : students) {

        System.out.println(albums.getAsJsonObject().get("LastChapelAttended"));

    }

}


查看完整回答
反對(duì) 回復(fù) 2021-09-03
?
慕姐4208626

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超7個(gè)贊

Students 是一個(gè) JsonArray LastChapelAttended 是一個(gè) JSONObject 你可以通過調(diào)用得到它


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


}


查看完整回答
反對(duì) 回復(fù) 2021-09-03
  • 3 回答
  • 0 關(guān)注
  • 361 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)