[英]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.