android对json排序,如何在android中对多个Jsonobjects的JsonObject进行排序

我正在开发一个Android应用程序,在我的一些代码中,我想按照降序中的一些值对JsonObject进行排序。

"smileys": {

"2": {

"image_icon": "http://devaddons1.socialengineaddons.com/mobiledemotesting/public/system/a2/39/01/1376a_0d1e.png?c=4a00",

"id": 2,

"count": 1

},

"3": {

"image_icon": "http://devaddons1.socialengineaddons.com/mobiledemotesting/public/system/a4/39/01/1376c_35c0.png?c=59a3",

"id": 3,

"count": 2

},

"4": {

"image_icon": "http://devaddons1.socialengineaddons.com/mobiledemotesting/public/system/a6/39/01/1376e_2019.png?c=f190",

"id": 4,

"count": 1

}

},

我想基于键值“count”对这个JsonObject进行排序,以便具有更高计数的JsonObject将首先出现,依此类推。

我已经使用以下代码尝试了它

ArrayList array = new ArrayList<>();

JSONArray smileyKeys = smileyArrays.names();

for (int i = 0; i < smileyArrays.length(); i++) {

try {

array.add(smileyArrays.getJSONObject(smileyKeys.optString(i)));

} catch (JSONException e) {

e.printStackTrace();

}

}

Collections.sort(array, new Comparator() {

@Override

public int compare(JSONObject lhs, JSONObject rhs) {

// TODO Auto-generated method stub

try {

if(lhs.optInt("count") < rhs.getInt("count") ){

return 1;

} else {

return -1;

}

} catch (JSONException e) {

// TODO Auto-generated catch block

e.printStackTrace();

return 0;

}

}

});

JSONArray jsonArray = new JSONArray(array);

JsonObject smileysSortedAdday = jsonArray.optJSONObject(0);

但它不能使用JsonObject,我无法在JsonArray中转换它。有人可以指导我如何实现这一目标。

非常感谢先进。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值