我正在编写一个应用程序,其中我使用JSON将数据提取到ListView中,并且我已经获取了第一级(eventName)的数据,但是在获取第二级(angelName)时获取空白活动...
JSON: -
[
{
"eventName" : "Honda Motors",
"angelList" : [
{
"angelID": "1",
"angelName": "Angel A"
},
{
"angelID": "2",
"angelName": "Angel B"
}
]
}
]
简而言之,无法获得AngelName,
AngelsActivity.java: -
public class AngelsActivity extends ListActivity {
// Connection detector
ConnectionDetector cd;
// Alert dialog manager
AlertDialogManager alert = new AlertDialogManager();
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jsonParser = new JSONParser();
ArrayList<HashMap<String, String>> angelsList;
// albums JSONArray
JSONArray arrayAngels = null;
// Category name
String category_Name;
// ALL JSON node names
private static final String ANGEL_NAME = "angelName";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
cd = new ConnectionDetector(getApplicationContext());
// Check for internet connection
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(AngelsActivity.this, "Internet Connection Error",
"Please connect to working Internet connection", false);
// stop executing code by return
return;
}
// Hashmap for ListView
angelsList = new ArrayList<HashMap<String, String>>();
// Loading Albums JSON in Background Thread
new LoadAlbums().execute();
// get listview
ListView lv = getListView();
/**
* Listview item click listener
* TrackListActivity will be lauched by passing album id
* */
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
}
});
}
/**
* Background Async Task to Load all Albums by making http request
* */
class LoadAlbums extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AngelsActivity.this);
pDialog.setMessage("Listing Angels ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting Categories JSON
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
String json = jsonParser.makeHttpRequest(EventsActivity.URL_CATEGORIES, "GET",
params);
try {
arrayAngels = new JSONArray(json);
if (arrayAngels != null) {
// looping through All albums
for (int i = 0; i < arrayAngels.length(); i++) {
JSONObject c = arrayAngels.getJSONObject(i);
// Storing each json item values in variable
String name = c.getString(ANGEL_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(ANGEL_NAME, name);
// adding HashList to ArrayList
angelsList.add(map);
}
}else{
Log.d("Angels: ", "null");
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all albums
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
AngelsActivity.this, angelsList,
R.layout.list_item,
new String[] {ANGEL_NAME},
new int[] {R.id.category_name });
// updating listview
setListAdapter(adapter);
}
});
}
}
}
答案 0 :(得分:1)
您似乎没有在正确的级别获取JSONArray。
您获得的JSONArray arrayAngels实际上是一个只包含一个元素的数组。要获得包含天使的数组,您需要更深入:
JSONArray allAngels = arrayAngels.getJSONObject(0).getJSONArray("angelList");
您从数据中获取的第一个JSONArray是一个包含所有内容的元素的数组。该数组中包含的第一个(也是唯一的)JSONObject仍包含您的所有数据。只有当你从该对象获得JSONArray时,才能得到你想要的东西。
编辑:完整的代码示例:
JSONObject jsonData = new JSONArray(json).getJSONObject(0);
arrayAngels = jsonData.optJSONArray("angelList");
if (arrayAngels != null) {
// Code to process arrayAngels
}
如果你想也使用它,jsonData也包含eventName数据。请注意,如果getJSONArray()无法找到您想要的密钥,则抛出异常,因此最好使用optJSONArray,如果密钥不在数据中,则返回null。
答案 1 :(得分:1)
试试这个,
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
String json = jsonParser.makeHttpRequest(EventsActivity.URL_CATEGORIES, "GET",
params);
try {
arrayAngels = new JSONArray(json).getJSONObject(0).getJSONArray("angelList");
if (arrayAngels != null) {
// looping through All albums
for (int i = 0; i < arrayAngels.length(); i++) {
JSONObject c = arrayAngels.getJSONObject(i);
// Storing each json item values in variable
String name = c.getString(ANGEL_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(ANGEL_NAME, name);
// adding HashList to ArrayList
angelsList.add(map);
}
}else{
Log.d("Angels: ", "null");
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
答案 2 :(得分:0)
试试这个..
for (int i = 0; i < arrayAngels.length(); i++) {
JSONObject c = arrayAngels.getJSONObject(i);
// Storing each json item values in variable
String name = c.getString(ANGEL_NAME);
JSONArray angels = c.getJSONArray("angelList");
for (int j = 0; j < angels.length(); j++) {
JSONObject c1 = arrels.getJSONObject(j);
Log.v("angelName",c1.getString(ANGEL_NAME));
}
}
答案 3 :(得分:0)
你必须首先获得Json数组“angelList”,然后才能获得天使名称值。
JSONArray arra = new JSONArray(json);
JSONObject jobj=arra.getJSONObject(0);
arrayAngels = jobj.getJSONArray("angelList");