Suppose, I have a Json & I want to get the value of particular field from the Json then How can i achieve that?
Please open below link to have a look at the json.
Json: https://jsoneditoronline.org/?id=05f3f2de21184ea19a105b4e5219c675
Now, I want to get values of all 'created' which we can find inside object->changelog->histories->0->created/1->created/2->created....30->created.
We can also refer left hand side json if required.
How can i get the value of created in java code? Please help me here.
You can use org.json, and with a JSONObject you can get what you want
JSONObject json = new JSONObject(yourjsonString);
---Add after comment ---
I propose you :
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
You can concat if you want.
Try by yourself to get exactly what you want.
Make a call on your url to retrieve your json content.
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;
}
}
Now , in jsonString we have all the json from url. Now let's retrieve a field(changelog field) from that json:
String changelog = extractFromJsonAStringWithKey(jsonString, "changelog");
private String extractFromJsonAStringWithKey(String jsonString, String key){
JSONObject jsonObject = new JSONObject(jsonString);
return (String) jsonObject.get(key);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.