0

I am firing a request through my android application using HttpPost , as the request is returning a JSON Callback function , i don't know how to handle it and parse it.

returned format:

handleEmployeeResponse({
  "records": [
    {
      "fullDesc": "Records for employe",
      "id": "Emp_1",
      "name": "Jack"
    }
  ]
});

in a firebug i can see as a response text

handleEmployeeResponse({"records":[{"fullDesc":"Records for employe","id":"Emp_1","name":"Jack"}]});

if i will parse the above response using JSONObject jObject = new JSONObject(jString); i am surely gonna get JSON parsing error as the above response is not valid json at all so i have to remove "handleEmployeeResponse , ( , ); " form the response string then i need to pass it so JSONObject can anyone tell me how to parse json with a callback function in android

5
  • 1
    Use Google -> google.com/… Commented Jul 21, 2011 at 12:32
  • Is handleEmployeeResponse a Java function or is the code you posted all received text? Commented Jul 21, 2011 at 12:32
  • @Hunt by returning a JSON Callback function, do you mean a JSON string i.e. the one you posted ? or a POJO ? Commented Jul 21, 2011 at 12:34
  • it is a java function that returns as a response text Commented Jul 21, 2011 at 12:35
  • @olamotte handleEmployeeResponse({"records":[{"fullDesc":"Records for employe","id":"Emp_1","name":"Jack"}]}); this is how it returns from the server Commented Jul 21, 2011 at 12:37

2 Answers 2

0

It looks like your service is returning a response in the JSONP format (JSON with Padding). You either need to regex out the JSON message, or find out a way to ask the service not to return the padding.

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

Comments

0

Have a look here : you should use the JSONTokener Class and thus get a JSONObject corresponding to your structure.

TOKENER

the example is pretty self-explanatory.

2 Comments

i am getting java.lang.ClassCastException: java.lang.String at JSONObject object = (JSONObject) new JSONTokener(responseString).nextValue()
@Hunt did you try with another JSON string ? Obviously the returned string as an overhead, and the valid JSON starts here : you should then truncate the string to : { "records": [ { "fullDesc": "Records for employe", "id": "Emp_1", "name": "Jack" } ] } if you have any doubt use JSONLint

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.