java - Why is parsing a single item String into a JSONArray failing -


i wondering why parsing code not working. trying responses webserver (get) request jasonarray. working multiple items (using get http://10.0.2.2:8000/exercises). if try receive single item (get http://10.0.2.2:8000/exercises/1) fails. found cause of problem. respjson = new jsonarray(resp); seems fail on string not surounded [ , ], can see sample output. can tell me doing wrong here? should use different method, designed simgle item requests handle this? use simple check first character in string, such hack, cant believe best way todo it.

(i posting minimal code of use + output)

httphost target = new httphost(host, port, "http"); httpget getrequest = new httpget(requestpath); getrequest.setheader("content-type", "application/json"); getrequest.setheader("accept", "application/json"); response = httpclient.execute(target, getrequest);  entity = response.getentity(); string resp = entityutils.tostring(entity); return new jsonarray(resp); 

using code on request http://10.0.2.2:8000/exercises produces following output string, can parsed jsonarray (leaving actual data in here, better comparsion):

[{"name": "ringe", "duration": "20", "repeats": "no repeats", "creationtime": "2014-06-07 19:24:53.206000", "pausetime": "3", "alarmsound": "default", "description": "this android description", "type": "repeats"}, {"name": "fingerkraft", "duration": "-1", "repeats": "10", "creationtime": "2014-06-07 18:08:03.576000", "pausetime": "20", "alarmsound": "default", "description": "asdasdasd", "type": "repeats"}, {"name": "ringe", "duration": "12", "repeats": "no", "creationtime": "2014-06-07 00:54:22.844000", "pausetime": "23", "alarmsound": "default", "description": "asdasdasd", "type": "duration"}] 

however, using method on http://10.0.2.2:8000/exercises/1 produces string

{"name": "ringe", "duration": "12", "repeats": "no", "creationtime": "2014-06-07 00:54:22.844000", "pausetime": "23", "alarmsound": "default", "description": "asdasdasd", "type": "duration"} 

which can't parsed jsonarray (return null). so, expected behaviour? built api (which rest) django-rest-framework.

 string jsonstr = getjson(); jsonobject json = new jsonobject(jsonstr); 

to create single json object.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -