0

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.

roeygol
  • 4,908
  • 9
  • 51
  • 88
leonev1
  • 5
  • 2
  • 1
    Print the value of data before trying to parse it, or use your debugger. It doesn't seem to contain the value you think it contains. – JB Nizet Aug 12 '18 at 09:05

1 Answers1

1

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.

Alien
  • 15,141
  • 6
  • 37
  • 57
  • Hi Alien. Thanks for the reply. I have checked the link you provided, however for some reason my new JSONObject(data) asks for no arguments, doesn't let me pass data into it. I'm pretty sure I have all the libraries here, but I am double checking... – leonev1 Aug 12 '18 at 16:44
  • Hi Alien. I got it now. Thank you for your help and have a great day! – leonev1 Aug 12 '18 at 18:49
  • Hi leonev1, Glad to see that it solved your problem. However you should also notice atleast who answered your question! – CodeYourBrain Aug 15 '18 at 13:54