[英]Empty JSON response when trying to parse JSONObject with one JSONArray
I got code that gets JSONArrays
, but however when I try to get JSONObject
that contains only one JSONArray
it gives me empty JSONArray
. 我有获取
JSONArrays
代码,但是,当我尝试获取仅包含一个JSONArray
JSONObject
,它给了我空的JSONArray
。
For example if I need to get data from this JSONObject
: 例如,如果我需要从此
JSONObject
获取数据:
{"events":[{"start":1357714800,"end":1357736400,"name":"Example1","description":""}]}
I get {"events":[]}
as JSONObject
, []
meaning that it doesn't contain any JSONArrays. 我将
{"events":[]}
作为JSONObject
, []
表示它不包含任何JSONArrays。 Also length of JSONObject
is in this case 0. But it doesn't throw any kind of Exceptions
. 同样,在这种情况下,
JSONObject
长度为0。但是它不会抛出任何Exceptions
。
but if JSONObject
contains multiple JSONArrays
like this: 但是,如果
JSONObject
包含多个JSONArrays
如下所示:
{"events":[{"start":1357714800,"end":1357736400,"name":"Example1","description":""},{"start":1357714600,"end":1357736500,"name":"Example2","description":""},{"start":1357514800,"end":1357536400,"name":"Example3","description":""}]}
then my code works perfect. 那么我的代码就完美了。
Here is the code I use to parse JSON: 这是我用来解析JSON的代码:
private void getObjects(String url) throws JSONException, Exception {
JSONObject jsonObject = new JSONObject(new NetTask().execute(url).get());
JSONArray job1 = jsonObject.getJSONArray("events");
System.out.println(jsonObject.toString());
System.out.println("JOB1 LENGTH: "+job1.length());
for (int i = 0; i < job1.length(); i++) {
JSONObject jsonEvent = job1.getJSONObject(i);
int start = jsonEvent.getInt("start");
int end = jsonEvent.getInt("end");
String name = jsonEvent.getString("name");
String description = jsonEvent.getString("description");
}
}
public class NetTask extends AsyncTask<String, Integer, String>
{
@Override
protected String doInBackground(String... params)
{
String jsonText = "";
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
reader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuffer buffer = new StringBuffer();
int read;
char[] chars = new char[1024];
while ((read = reader.read(chars)) != -1) {
buffer.append(chars, 0, read);
}
jsonText = buffer.toString();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return jsonText;
}
}
is there something wrong that I am missing or is this normal behaviour? 我有什么不对的地方吗?还是这是正常现象?
I tried your given code (though I just made the AsyncTask just return the single-array string, and had to replace the opptunti.getString() stuff with jsonEvent.getString()). 我尝试了给定的代码(尽管我只是使AsyncTask只是返回单数组字符串,并且不得不用jsonEvent.getString()替换opptunti.getString()东西)。 It worked fine, aside from the fact that you're probably blocking the UI thread to wait for the server response.
除了您可能正在阻塞UI线程以等待服务器响应之外,它还可以正常工作。
My guess is the problem is that you are hitting the wrong URL, that the parameters are wrong or something like that. 我的猜测是问题在于您输入了错误的URL,参数错误或类似的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.