I have rest call and I want to get one of the key values from the JSON response, for example the value of "employeeID".
This is the code for the rest call:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
public class App {
public static String username = "Test_Username";
public static void main(String[] args) {
try {
URL url = new URL ("http://api.com/" + username);
String encoding = Base64.getEncoder().encodeToString(":".getBytes("utf-8"));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setRequestProperty ("Authorization", "Basic " + encoding);
InputStream content = (InputStream)connection.getInputStream();
BufferedReader in =
new BufferedReader (new InputStreamReader (content));
String line = "";
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
And this is a sample of the JSON response:
[{
"user": {
"userID": "Adam555",
"name": "Test Account",
"id": 1055287,
"recentApps": null,
"employeeID": "2991KDS"
},
"user_state": {
"EmployeeLookUpDBId": 1055304,
"userID": "Adam555",
"username": "Adam555"}]
So how can I get it in System.out.println for example saying "2991KDS"?