java - 为什么我的数组未显示为已过滤?
问题描述
我正在尝试使用此示例按价格从最低到最高对这个 JSON 对象进行 排序 如何在 java 中对 JSON 对象进行排序?. 但是当我尝试显示我的列表时,它似乎没有排序。我不确定我做错了什么,有人能指出我正确的方向。我已经阅读了几篇文章,他们都说要使用集合对 JSON 对象进行排序,我尝试在下面实现它,但它不起作用任何建议都值得赞赏。
json:-
[ {
"symbol" : "SAN",
"companyName" : "Banco Santander, S.A.",
"marketCap" : 62296657920,
"sector" : "Financial Services",
"industry" : "Banks—Diversified",
"beta" : 1.74298500000000000653699316899292171001434326171875,
"price" : 3.5800000000000000710542735760100185871124267578125,
"lastAnnualDividend" : 0.24899999999999999911182158029987476766109466552734375,
"volume" : 4284228,
"exchange" : "New York Stock Exchange",
"exchangeShortName" : "NYSE",
"country" : "ES",
"isEtf" : false,
"isActivelyTrading" : true
}, {
"symbol" : "ABEV",
"companyName" : "Ambev S.A.",
"marketCap" : 48729493504,
"sector" : "Consumer Defensive",
"industry" : "Beverages—Brewers",
"beta" : 0.93046200000000001129052407122799195349216461181640625,
"price" : 3.0099999999999997868371792719699442386627197265625,
"lastAnnualDividend" : 0.0970000000000000028865798640254070051014423370361328125,
"volume" : 24947634,
"exchange" : "New York Stock Exchange",
"exchangeShortName" : "NYSE",
"country" : "BR",
"isEtf" : false,
"isActivelyTrading" : true
}, {
"symbol" : "LYG",
"companyName" : "Lloyds Banking Group plc",
"marketCap" : 41814462464,
"sector" : "Financial Services",
"industry" : "Banks—Regional",
"beta" : 1.483138999999999985135445967898704111576080322265625,
"price" : 2.310000000000000053290705182007513940334320068359375,
"lastAnnualDividend" : 0.07022699999999999775912584709658403880894184112548828125,
"volume" : 5825636,
"exchange" : "New York Stock Exchange",
"exchangeShortName" : "NYSE",
"country" : "GB",
"isEtf" : false,
"isActivelyTrading" : true
},
代码 :-
public void downloadChangePercentage(String api) {
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
Request.Method.GET,
api,
null,
new Response.Listener<JSONArray>() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onResponse(JSONArray response) {
// ArrayList<JSONObject> array = new ArrayList<JSONObject>();
ArrayList<JSONObject> jsonStockList = new ArrayList<>();
for (int i = 0; i < response.length(); i++) {
// LOOP over JSON objects and added it to mode/ Stocklist arraylist..
try {
stockInfo = response.getJSONObject(i);
stockDetail = new StockDetails(stockInfo.getString("changesPercentage"), stockInfo.getString("symbol"), stockInfo.getString("price"), stockInfo.getString("name"));
stockListArrayList.add(stockDetail);
jsonStockList.add(response.getJSONObject(i));
searchStockAdapter.notifyDataSetChanged();
ArrayList<JSONObject> sortedStockList = sortJSONObject(jsonStockList);
for (int j = 0; j < sortedStockList.size(); j++) {
logText("sortedData", String.valueOf(sortedStockList.get(j)));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("error", error.toString());
}
});
// Add JsonArrayRequest to the RequestQueue
requestQueue.add(jsonArrayRequest);
}
ArrayList<JSONObject> sortJSONObject(ArrayList<JSONObject> jsonStockList){
Collections.sort(jsonStockList, new Comparator<JSONObject>() {
@Override
public int compare(JSONObject o1, JSONObject o2) {
try {
return Double.compare(o1.getDouble("price"), o2.getDouble("price"));
} catch (JSONException e) {
e.printStackTrace();
return 0;
}
}
});
return jsonStockList;
}
解决方案
引用您的代码并添加排序逻辑
void callApis(){
// Initialize a new JsonArrayRequest instance
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
Request.Method.GET,
api,
null,
new Response.Listener<JSONArray>() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onResponse(JSONArray response) {
/******** Do exactly like below **********/
ArrayList<JSONObject> jsonStockList = new ArrayList<>();
for (int i = 0; i<response.length();i++){
try {
jsonStockList.add(response.getJSONObject(i));
} catch (JSONException e) {
e.printStackTrace();
}
}
/** pass this stockListArrayList(sorted Data) in view/adapter */
for (JSONObject stockInfo : sortJSONObject(jsonInfosList)){
// assigning sorted data in data model
StockDetails stockDetail = new StockDetails();
stockDetail.setCompanyName(stockInfo.getString("companyName"));
stockDetail.setIndustry(stockInfo.getString("industry"));
stockDetail.setPrice(stockInfo.getDouble("price"));
stockDetail.setMarketCap(stockInfo.getLong("marketCap"));
stockDetail.setSector(stockInfo.getString("sector"));
stockDetail.setSymbol(stockInfo.getString("symbol"));
// below (stockListArrayList) is the list which you need to pass in your adapter and notifyDataSetChanged()
stockListArrayList.add(stockDetail);// stockListArrayList is already decleare in your code
}
/*********** end **********/
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Do something when error occurred
Log.i("error", error.toString());
}
}
);
// Add JsonArrayRequest to the RequestQueue
requestQueue.add(jsonArrayRequest);
}
根据价格对对象进行排序的功能
ArrayList<JSONObject> sortJSONObject(ArrayList<JSONObject> jsonStockList){
Collections.sort(jsonStockList, new Comparator<JSONObject>() {
@Override
public int compare(JSONObject o1, JSONObject o2) {
try {
return Double.compare(o1.getDouble("price"), o2.getDouble("price"));
} catch (JSONException e) {
e.printStackTrace();
return 0;
}
}
});
return jsonStockList;
}
推荐阅读
- html - 如何通过使用jquery单击另一个html页面上的元素来设置一个html页面的输入值?
- php - PHP 单元 - 模拟类
- javascript - Ajax 发布表单警报
- java - 将 csv 数据字符串转换为 json - Java - Play 框架
- svg - 如何更改svg中字符的宽度
- css - 无法在阴影根离子 5 内应用 css
- flutter - Flutter - 如何更改 LicensePage 的背景颜色?
- node.js - POST 对象也包含数据库中的数组(反应,nodejs)
- java - 我怎样才能得到简单的 yyyy-MM-dd 这个日期?
- python - 无法在 pdf 的所有面上留出 2.5 厘米的边距