Reputation: 5
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.
Upvotes: 0
Views: 1795
Reputation: 201
Looks like you are parsing response incorrectly. There are 2 things to be done here.
1)Make sure you obtain JSON data correctly from your response. You may follow this link Get a JSON object from a HTTP response for getting stringified JSON data from your response.
2)Play around with your JSON to get specific values out of it. Your getValue()
method could be used for that purpose with little modifications. I have made some changes to your method and here is the updated code for the same:
public static void getValue(String data) {
try {
JSONObject responseObject = new JSONObject(data);
JSONArray jsonArray = (JSONArray) responseObject.get("data");
JSONObject jsonObject = (JSONObject) jsonArray.get(0);
String result = jsonObject.getString("password");
System.out.println(result);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I have tested it with your data and it working as per the expectation, printing 1234578566 over console.
Note : I have used org.json lib here and not simple json.
Hope this helps.
Thanks.
Upvotes: 1