I am sending a POST request to a server. The server responds with the following (JSON):
{"data":[
{
"password":"1234578566",
"status":"processing"
}
],
"status":200
}
This is my POST Method:
public static void sendPostRequest(String conversion) throws IOException, AuthenticationException {
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("https://url.com");
httpPost.setEntity(new StringEntity(conversion));
UsernamePasswordCredentials credentials =
new UsernamePasswordCredentials("username", "password");
httpPost.addHeader(new BasicScheme().authenticate(credentials, httpPost, null));
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpResponse response = client.execute(httpPost);
String data = EntityUtils.toString(response.getEntity());
client.close();
// System.out.println(data);
}
Please note the String "data" is the server responding with the JSON data above. Now, I am trying to get the password attribute from the data.
public static void getValue(String data) throws ParseException {
JSONObject object = (JSONObject) new JSONParser().parse(data);
JSONArray array = (JSONArray) object.get("data");
JSONObject attribute = (JSONObject) array.get(0);
JSONObject userData = (JSONObject) attribute.get("password");
String result = userData.toString();
System.out.println(result);
}
Exception in thread "main" Unexpected character (T) at position 0.
at org.json.simple.parser.Yylex.yylex(Unknown Source)
at org.json.simple.parser.JSONParser.nextToken(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
I am getting this exception, but I wonder why? Have tried to change here and there but with no success.
These are my imports:
import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.parser.ParseException;
import org.json.simple.parser.JSONParser;
Thank you.