我是新的JSON android eclipse。我正在使用类似于以下教程的图像进行列表视图:http://www.wingnity.com/blog/android-json-parsing-and-image-loading-tutorial/。本教程包含一个包含数组名称的JSON数组。
我的JSON数组不包含数组名称。我现在的问题是如何在没有数组名称的情况下编写JSON数组:
以下是我的JSON代码:
[
{
"ID":0,
"Title":"Malokase backs Mabena to shine",
"thumbURL":"http://www.testing.co.za/App_Images/n00001_thumb.jpg",
"imgURL":"http://www.testing.co.za/App_Images/n00001.jpg",
"Story":"Mabena had a rough time with Orlando Pirates, where expectations from the Ghost weighed heavily on him. But at Stars, the pressure has eased up to the extent that he scored on debut against Mamelodi Sundowns in the MTN8. His goal helped Dikwena beat the Brazilians 2-1 to advance to the semifinals, where they will face Kaizer Chiefs.\\n\\n“Ndumiso has always been a good player,” Malokase said. “It was a good thing for him to start with a goal. I think it will boost his confidence and motivate him to do even better for the club going forward.” Mabena will fill the void left by Siphelele Mthembu, who joined Chiefs. Malokase, a former Pirates striker, advised Mthembu on how to deal with the pressure . “He (Mthembu) has been there (at Chiefs),” Malokase said. \\n\\n“He (Mthembu) has been there (at Chiefs),” Malokase said. “He has been at Pirates too. The most important thing is to put his foot on the ground, not to put himself under pressure, but he should do the business when given a chance.” ",
"Date":"2014-08-07T00:00:00"
},
{
"ID":1,
"Title":"New Signings",
"thumbURL":"http://www.testing.co.za/App_Images/n00002_thumb.jpg",
"imgURL":"http://www.testing.co.za/App_Images/n00002.jpg",
"Story":"Dikwena signs 8 new players during Kit Launch.\\n\\n1. Ndumiso Mabena\\n2. Lucky Nguzana\\n3. Marothi Diale\\n4. Rhulani Manzini\\n5. Joseph Banyane\\n6. Isaac Nhlapo\\n7. Letladi Madubanya\\n8. Patrick Kaunda",
"Date":"2014-08-07T00:00:00"
}
]
我试图解析JSON的代码如下:
String result = "";
protected Boolean doInBackground(String... urls) {
try {
// ------------------>>
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray(result);
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
Article article = new Article();
article.setTitle(object.getString("Title"));
article.setStory(object.getString("Story"));
article.setThumbURL(object.getString("thumbURL"));
articleList.add(article);
}
return true;
}
// ------------------>>
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
dialog.cancel();
adapter.notifyDataSetChanged();
if (result == false)
Toast.makeText(getActivity().getApplicationContext(),
"Unable to fetch data from server", Toast.LENGTH_LONG)
.show();
}
当我运行代码时,列表视图未填充,然后进入onPostExecute方法。
答案 0 :(得分:1)
摆脱:
JSONObject jsono = new JSONObject(data);
写信:
JSONArray jarray = new JSONArray(data);
应该足够了。
答案 1 :(得分:1)
从您的班级替换以下两行代码
JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray(result);
这
JSONArray jarray = new JSONArray(data);
这就足够了
答案 2 :(得分:0)
谢谢大家,我最后对推荐的建议做了什么改变了以下内容:
@Override
protected Boolean doInBackground(String... urls) {
try {
// ------------------>>
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
// JSONObject jsono = new JSONObject();
JSONArray jarray = new JSONArray(data);
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
Article article = new Article();
article.setTitle(object.getString("Title"));
article.setStory(object.getString("Story"));
article.setThumbURL(object.getString("thumbURL"));
articleList.add(article);
}
return true;
}
// ------------------>>
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
dialog.cancel();
adapter.notifyDataSetChanged();
if (result == false)
Toast.makeText(getActivity().getApplicationContext(),
"Unable to fetch data from server", Toast.LENGTH_LONG)
.show();
}
}
答案 3 :(得分:0)
添加.toString()方法
JSONArray jarray = jsono.getJSONArray(result.toString());