0

I am trying to convert following json string to JSONObject in android. But it throws JSONException. I have been digging up my mind for past few hours. Please help.

JSON String :

parseExchangeRate({"query":
             {"count":1,"created":"2012-09-07T18:49:32Z","lang":"en-US","results":
                       {"row":{"rate":"55.395","name":"USD to INR"}}}});

Exception:

Value <jsonobject>parseExchangeRate( of type java.lang.String cannot be converted to JSONObject

Code :

String result = convertStreamToString(instream);
Log.d(TAG, result); //this outputs the above stated string
JSONObject json = new JSONObject(result); // this line thows exception

Thanks in advance.

1
  • Loose the 'parseExchangeRate()' Commented Sep 7, 2012 at 19:15

1 Answer 1

2

what is the JSON string? you state that it's,

parseExchangeRate({"query":
              {"count":1,"created":"2012-09-07T18:49:32Z","lang":"en-US","results":
                        {"row":{"rate":"55.395","name":"USD to INR"}}}});

but that looks like a line of javascript code. JSON != javascript. it looks like your web service is passing back a JSONP response. that's fine, but since you are not a javascript client, you need to parse out the actual JSON string from that response.

String result = convertStreamToString(instream);
Pattern p = Pattern.compile("^.*?\\((.*?)\\);$", Pattern.DOTALL);
Matcher m = p.matcher(result);
if (m.matches()) {
  String json = m.group(1);
  JSONObject jo = new JSONObject(json);
  ...
} else {
  // whoops
}
Sign up to request clarification or add additional context in comments.

Comments

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.