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

Hunt
  • 8,215
  • 28
  • 116
  • 256
  • 1
    Use Google -> http://www.google.com/search?aq=f&sourceid=chrome&ie=UTF-8&q=parse+JSON+android – Wroclai Jul 21 '11 at 12:32
  • Is handleEmployeeResponse a Java function or is the code you posted all received text? – Zoran Zaric Jul 21 '11 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 ? – olamotte Jul 21 '11 at 12:34
  • it is a java function that returns as a response text – Hunt Jul 21 '11 at 12:35
  • @olamotte handleEmployeeResponse({"records":[{"fullDesc":"Records for employe","id":"Emp_1","name":"Jack"}]}); this is how it returns from the server – Hunt Jul 21 '11 at 12:37

2 Answers2

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.

olamotte
  • 917
  • 9
  • 20
  • i am getting java.lang.ClassCastException: java.lang.String at JSONObject object = (JSONObject) new JSONTokener(responseString).nextValue() – Hunt Jul 21 '11 at 14:24
  • @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](http://jsonlint.com/) – olamotte Jul 21 '11 at 18:08
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.

Mark Fisher
  • 9,838
  • 3
  • 32
  • 38