[英]How do i get particular field from the Json in Java?
您可以使用 org.json,並使用 JSONObject 您可以獲得您想要的
JSONObject json = new JSONObject(yourjsonString);
---在評論后添加---
我建議你:
JSONObject json = new JSONObject(yourjsonString);//it's object
JSONObject changelog = json.getJSONObject("changelog");
JSONArray histories = changelog.getJSONArray("histories");
histories.getJSONObject(0); //first json in histories
如果你願意,你可以連接。
親自嘗試以得到您想要的東西。
調用您的 url 以檢索您的 json 內容。
String jsonString = requestInfoFromUrl(urlLink);
private String requestInfoFromUrl(String url) throws IOException {
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url);
HttpGet getMethod = new HttpGet(builder.build().toUri());
HttpResponse response = httpClient.execute(getMethod);
if (response.getStatusLine().getStatusCode() == 200){
return EntityUtils.toString(response.getEntity());
} else {
LOG.error("Error appear while request response from an url.");
return null;
}
}
現在,在 jsonString 中,我們擁有來自 url 的所有 json。 現在讓我們從該 json 中檢索一個字段(changelog 字段):
String changelog = extractFromJsonAStringWithKey(jsonString, "changelog");
private String extractFromJsonAStringWithKey(String jsonString, String key){
JSONObject jsonObject = new JSONObject(jsonString);
return (String) jsonObject.get(key);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.