I have this code that return a response from an API provider in the format of JSON Object. Below is the code:
public class Authentication {
public static void main(String[] args) {
try {
URL url = new URL ("https://xxxxx.com");
String encoding = "xxxxxxxxx";
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
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();
}
}
}
The above code will return a response like this:
{
"accessToken": "0.AQAAAUmdztB3AAAAAAAEkvg",
"tokenType": "Bearer",
"expiresIn": 300
}
So here is the problem, I'm trying to get the value of accessToken only so that i can use it to make an api call. I've tried this:
String loudScreaming = JSONML.toJSONObject("accessToken").getString("accessToken");
But it will return null. I've read about Jackson but don't have any idea on how to do it. Please guide me. Thanks in advance.
lineinstead of"accessToken"as a parameter totoJSONObject(...).