[英]Why is parsing a single item String into a JSONArray failing
I was wondering why my parsing code is not working. 我想知道为什么我的解析代码无法正常工作。 I am trying to get responses from a webserver (
GET
) request into a JASONArray
. 我正在尝试将来自Web服务器(
GET
)请求的响应放入JASONArray
。 This is already working for multiple items (using GET http://10.0.2.2:8000/exercises
). 这已经适用于多个项目(使用
GET http://10.0.2.2:8000/exercises
)。 But if I try to receive a single item ( GET http://10.0.2.2:8000/exercises/1
) it fails. 但是,如果我尝试接收单个项目(
GET http://10.0.2.2:8000/exercises/1
),它将失败。 I already found the cause of the problem. 我已经找到问题的原因。
respJSON = new JSONArray(resp);
seems to fail on a String that is not surounded by [
and ]
, as you can see from my sample output. 从我的示例输出中可以看出,似乎在未由
[
和]
包围的String上失败了。 Can anybody tell me what I am doing wrong here? 有人可以告诉我我在做什么错吗? Should I use a different method, specifically designed for simgle item requests to handle this?
我是否应该使用专门为模拟项目请求而设计的其他方法来处理此问题? I could use a simple check for the first character in the string, but that is such a hack, that I cant believe this is the best way todo it.
我可以对字符串中的第一个字符进行简单的检查,但这真是太糟糕了,我无法相信这是最好的方法。
(I am only posting the very minimal code of what I use + the 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 this code on a request like http://10.0.2.2:8000/exercises
produces the following output String, which can be parsed into a JSONArray
(leaving the actual data in here, for better comparsion): 在诸如
http://10.0.2.2:8000/exercises
之类的请求上使用此代码会产生以下输出String,可以将其解析为JSONArray
(将实际数据保留在此处,以进行更好的比较):
[{"name": "Ringe", "duration": "20", "repeats": "no repeats", "creationtime": "2014-06-07 19:24:53.206000", "pausetime": "3", "alarmsound": "default", "description": "this is 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 this method on http://10.0.2.2:8000/exercises/1
produces a String like 但是,在
http://10.0.2.2:8000/exercises/1
上使用此方法会产生类似
{"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 be parsed into a JSONArray
(return is null). 不能解析为
JSONArray
(返回null)。 So, is this the expected behaviour? 那么,这是预期的行为吗? I built the API (which is obviously rest) with the django-rest-framework.
我用django-rest-framework构建了API(显然是静止的)。
String jsonStr = getJson();
JSONObject json = new JSONObject(jsonStr);
To create a single Json object. 创建单个Json对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.