[英]Java: Array of Arrays from JSON
我有一個像這樣的 JSON Arrays of Array
"results": [
{
"id": "AAA",
"color": "#4D4837",
"links": {
"self": "https://aaa.com",
"html": "https://bbb.com",
"download": "https://ccc.com",
"download_location": "ddd.com"
},
"categories": [],
"likes": 3891,
},
{
"id": "BBB",
"color": "#4D453",
"links": {
"self": "https://abb.com",
"html": "https://bcc.com",
"download": "https://ccc.com",
"download_location": "ddd.com"
},
"categories": [],
"likes": 3000,
}
]
我想檢索“html”的“ https://bbb.com ”和“ https://bcc.com ”,但我不知道該怎么做。
基於善意的評論,我提出以下內容。 不知何故,無法放置“getJSONObject()”。 錯誤消息顯示“無法解析 'JSONArray' 中的方法 'getJSONObject'”。
JSONArray array = new JSONArray((Collection) jobjt.get("Strings"));
for (int i =0 ; i<2 ; i++){
JSONObject job = (JSONObject) array.get(i); --> get(i) can not be changed to getJSONObject(i)
String id = job.get("id").toString();
String color = job.get("color").toString();
String photoUrl = job.get("links").toString(); --> By updating here, I want to store only "https://bbb.com" and "https://bcc.com".
}
但是當我嘗試使用以下內容時,不僅檢索“html”,還檢索“self”和其他信息。
String photoUrl = job.get("links").toString();
請告訴我如何只檢索“html”。
我正在使用 IntelliJ。
要遵循的步驟(假設你有你提到的正確 JSONArray):
index=0
links
鍵獲取內部 JSONObjecthtml
鍵訪問您的內容您的案例示例:
JSONObject indexedObject = jsonArray.getJSONObject(0);
JSONObject linksObject = indexedObject.getJSONObject("links");
String html= linksObject.getString("html");
最好按照 Harshal 的建議繼續檢查密鑰是否存在
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.