Java 我已经将我的jsonObject转换为JsonArray,如何循环它 我正在尝试转换我成功获得的jsonArray 但是如何循环我的jsonArray以便获得完整的细节 在listview中。如果有多个数据,如何添加数据 并将其解析为listview。
我已将jsonObject转换为JsonarrayJava 我已经将我的jsonObject转换为JsonArray,如何循环它 我正在尝试转换我成功获得的jsonArray 但是如何循环我的jsonArray以便获得完整的细节 在listview中。如果有多个数据,如何添加数据 并将其解析为listview。,java,android,json,Java,Android,Json,我已将jsonObject转换为Jsonarray public class getData extends AsyncTask<String, String, String> { @Override protected String doInBackground(String... params) { return JsonParser.makeHttpUrlConnectionRequest(url, getParam()); }
public class getData extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
return JsonParser.makeHttpUrlConnectionRequest(url, getParam());
}
private List<NameValuePair> getParam() {
SharedPreferences preferences = ReportActivity.this.getSharedPreferences(getString(R.string.detail), MODE_PRIVATE);
enroll_no = preferences.getString(getString(R.string.loginenroll), "");
List<NameValuePair> list = new ArrayList<>();
list.add(new BasicNameValuePair("enroll_no", enroll_no));
// Log.e("List", String.valueOf(list.add(new BasicNameValuePair("enroll_no",enroll))));
Log.e("Request in Profile", String.valueOf(list));
return list;
}
@Override
protected void onPostExecute(String httpresponse) {
super.onPostExecute(httpresponse);
if (httpresponse == null) {
Toast.makeText(ReportActivity.this, "Unable to Connect", Toast.LENGTH_SHORT).show();
} else if (httpresponse.equals("error")) {
Toast.makeText(ReportActivity.this, "Internal Server Error", Toast.LENGTH_SHORT).show();
} else {
try {
responseParameter = new JSONObject(httpresponse);
JSONObject fees_history = responseParameter.getJSONObject("fees_history");
// JSONObject RC1 = fees_history.getJSONObject("RC1");
Iterator rc1 = fees_history.keys();
JSONArray jsonArray = new JSONArray();
int i = 0;
while (rc1.hasNext()) {
String key = (String) rc1.next();
jsonArray.put(fees_history.get(key));
JSONObject object = jsonArray.getJSONObject(i++);
JSONObject heading = object.getJSONObject("heading");
JSONArray details = object.getJSONArray("details");
Log.e("details", details.toString());
String Amount = heading.getString("Amount");
String date = heading.getString("date");
String Reciept = heading.getString("Reciept");
// for (int j = 0; j < details.length(); j++) {
JSONObject detail = details.getJSONObject(i);
String Month = detail.getString("Month");
String Amountt = detail.getString("Amount");
Log.e("Amount", Amountt);
Log.e("Month", Month);
Unpaid_fees_detail unpaid_fees_detail = new Unpaid_fees_detail(Reciept, date, Amount,Amountt,Month);
unpaid_fees_details.add(unpaid_fees_detail);
}
} catch (JSONException e) {
e.printStackTrace();
}
// Log.e("RC1",RC1.toString());
// Log.e("fees_history", fees_history.toString());
}
Unpaid_fees_detail_adapter unpaid_fees_detail_adapter = new Unpaid_fees_detail_adapter(ReportActivity.this, unpaid_fees_details);
listView.setAdapter(unpaid_fees_detail_adapter);
}
}
你只是为了做一个fori loop
您创建了:
JSONArray details=object.getJSONArraydetails
所以我不明白:
JSONObject detail=details.getJSONObject
从此行更改代码:
JSONArray details=object.getJSONArraydetails
比如:
JSONArray details = response.getJSONArray("details");
// List Of Custom Object Class 'DetailItem'
// It Contains fields of details object , unique_id and Reciept and etc
ArrayList<DetailItem> detailItems = new ArrayList<>();
if (details != null) {
for (int k = 0; k < details.length(); k++) {
JSONObject jsonObject = details.getJSONObject(k);
String uniqueId = jsonObject.getString("unique_id");
String reciept = jsonObject.getString("Reciept");
// Your Code ...
DetailItem detailItem = new DetailIte();
detailItem.setUniqueId(uniqueId);
detailItem.setReciept(reciept);
// Your Code ...
detailItems.add(detailItem);
// Your Code ...
}
}
我已经这样做了,但数据并没有依次显示,它会循环很多次如果没有依次显示,可能是您的用户foreach!它显示了各种各样的数据。请检查它。我认为未付费用的详细信息是您需要的列表。是的,未付费用列表应以不同的类型显示在费用收据rc1或rc2 More下。我建议查看Gson库并进行改装
ArrayList<DetailItem> detailItems = new ArrayList<>();
JSONArray details = response.getJSONArray("details");
if (details != null) {
for (int k = 0; k < details.length(); k++) {
JSONObject jsonObject = details.getJSONObject(k);
String uniqueId = jsonObject.getString("unique_id");
String reciept = jsonObject.getString("Reciept");
// Your Code ...
DetailItem detailItem = new DetailIte();
detailItem.setUniqueId(uniqueId);
detailItem.setReciept(reciept);
// Your Code ...
detailItems.add(detailItem);
// Your Code ...
}
}
JSONArray details = response.getJSONArray("details");
// List Of Custom Object Class 'DetailItem'
// It Contains fields of details object , unique_id and Reciept and etc
ArrayList<DetailItem> detailItems = new ArrayList<>();
if (details != null) {
for (int k = 0; k < details.length(); k++) {
JSONObject jsonObject = details.getJSONObject(k);
String uniqueId = jsonObject.getString("unique_id");
String reciept = jsonObject.getString("Reciept");
// Your Code ...
DetailItem detailItem = new DetailIte();
detailItem.setUniqueId(uniqueId);
detailItem.setReciept(reciept);
// Your Code ...
detailItems.add(detailItem);
// Your Code ...
}
}