0

I have am trying to convert my response from a POST to JSON. And here is what I am doing:

InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuilder response = new StringBuilder();
while((line = rd.readLine()) != null) {
    response.append(line).append("\r");
}
rd.close();
Log.i(TAG, response.toString());
JSONObject jsonObject = new JSONObject(response.toString());

But then I get error java.lang.String cannot be converted to JSONObject

But if I make a string, String string; and then I paste the what I logged and set it equal to string, and then try

JSONObject jsonObject = new JSONObject(string);

It works, so why is it working when I paste the logged response, but not when I just use response.toString();?

In the logcat it looks like this {"url": "www.google.com"}. And then when I paste it into string = "{\"url\": \"www.google.com\"}";

Thanks for the help

16
  • Can you show your response please Commented Jun 30, 2015 at 6:18
  • Please post the Json response you are getting from the server. Commented Jun 30, 2015 at 6:21
  • Where do you get the error you describe? Is it a compile-time error? An execution-time error? Which line? Have you tried not including the \r between lines? Is it possible that there's an unprintable character somewhere in the data? Commented Jun 30, 2015 at 6:23
  • Maybe after executing of this line in the while loop response.append(line).append("\n"); you become some unexpected new lines in your response and the JSONObject doesn't know how to manage them and maybe when you paste the response from logcat, you eliminate these lines. Try to add .trim() to the response.toString() Commented Jun 30, 2015 at 6:23
  • @JonSkeet is it execution-time error on the line JSONObject jsonObject = new JSONObject(response.toString()); Commented Jun 30, 2015 at 6:23

2 Answers 2

2

I met this problem before, try this:

new JSONObject(json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1));

or

if (response != null && response.startsWith("\ufeff")) {  
     in = in.substring(1);  
}  

the response from server contains a BOM header

Sign up to request clarification or add additional context in comments.

1 Comment

what if it is longer than 1 character? or isnt alwasys an "a", as sometimes its longer or a different character?
0
public JSONObject(java.lang.String source)
           throws JSONException

Construct a JSONObject from a source JSON text string.

source - A string beginning with { (left brace) and ending with } (right brace).

So while doing response.toString() if the string do not start with { and close with } then it will throw Exception.

3 Comments

okay but how can, I either jsut retun the JSON, or remove the begining string
@jamesbrown you can do something like this String result = "{" + response.substring(response.indexOf("{") + 1, response.indexOf("}")) + "}"; to remove begning String.
and then pass the result to JSONObject

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.