JSONArray中按某字段排序,解决JSONObject.fromObject()报错问题

这段代码演示了如何使用Java对JSON数组按照特定字段进行排序。它创建了一个Comparator,根据提供的`sortKey`字段(这里是'name')和`is_desc`标志(决定是升序还是降序)对JSONObject列表进行排序。代码适用于字符串类型的字段,但可以修改以适应其他类型的字段,如Double。

如果需要对json数组按JSON的某个字段大小排序,例如下面段json,想要按json数组sites中的name字段排序

"sites": [
        { "name":"Google", "info":[ "Android", "Google 搜索", "Google 翻译" ] },
        { "name":"Runoob", "info":[ "菜鸟教程", "菜鸟工具", "菜鸟微信" ] },
        { "name":"Taobao", "info":[ "淘宝", "网购" ] }
    ]
    /**
     * 对json数组排序,
     * @param jsonArr
     * @param sortKey 排序关键字
     * @param is_desc is_desc-false升序列  is_desc-true降序 (排序字段为字符串)
     * @return
     */
    public static String jsonArraySort(JSONArray jsonArr,String sortKey,boolean is_desc) {
        //存放排序结果json数组
        JSONArray sortedJsonArray = new JSONArray();
        //用于排序的list
        List<JSONObject> jsonValues = new ArrayList<JSONObject>();
        //将参数json数组每一项取出,放入list
        for (int i = 0; i < jsonArr.size(); i++) {
            jsonValues.add(JSONObject.fromObject(jsonArr.getJSONObject(i)));
        }
        //快速排序,重写compare方法,完成按指定字段比较,完成排序
        Collectio
@Repository("NbTkOrderService") public class NbTkOrderService extends AbstractGetOrderService { private static Logger log = LoggerFactory.getLogger(NbTkOrderService.class); @Autowired private TempSscApplyGoodsLinesManager tempSscApplyGoodsLinesManager; @Override public String getOutOrderXML(String applyId, String startDate, String endDate,Map<String, Object> map) throws Exception { //获取配置信息 String platformId = YbmUtil.queryPlatformId("TKSRMPT", "4"); //基础信息 String nbTkUrl = SysContantUtil.NB_TK_URL; String nbTkUserId = SysContantUtil.NB_TK_USER_ID; String nbTkPas = SysContantUtil.NB_TK_PW; String nbTkHos = SysContantUtil.NB_TK_HOS; JSONObject orderDataJson = new JSONObject(); orderDataJson.put("supcode",nbTkHos); orderDataJson.put("startdate",startDate); orderDataJson.put("enddate",endDate); JSONObject sendObj = new JSONObject(); sendObj.put("sendmain", orderDataJson); String sendXml = NbTkUnit.createSendXml(sendObj.toString(), "srm.sup.order.download"); log.info("宁波泰康获取订单数据请求参数" + sendXml); //获取订单 String returnBody = HttpUtil.httpSendToWebService(nbTkUrl, sendXml); log.info("宁波泰康获取订单数据返回结果" + returnBody); com.alibaba.fastjson.JSONObject responseJson = YbmUtil.xmlToJson(returnBody); JSONArray jsonArray = JSONArray.fromObject(responseJson.getString("Body")); if (ObjectUtils.isEmpty(jsonArray)){ return "暂无需要获取的订单数据! "; } String dataExchangeResponse = JSONObject.fromObject(jsonArray.get(0)).getString("DataExchangeResponse"); if (ObjectUtils.isEmpty(dataExchangeResponse)){ return "暂无需要获取的订单数据! "; } String dataExchangeResult = JSONObject.fromObject(JSONArray.fromObject(dataExchangeResponse).get(0)).getString("DataExchangeResult"); if (ObjectUtils.isEmpty(dataExchangeResult)){ return "暂无需要获取的订单数据! "; } JSONObject codeObj = JSONObject.fromObject(JSONObject.fromObject(dataExchangeResult).getString("recvmain")); String code =codeObj.getString("totalhandlestate"); if (ObjectUtils.isEmpty(dataExchangeResult) || !"1".equals(code)){ return "暂无需要获取的订单数据 或 获取数据失败" +codeObj.getString("totalhandlemessage") ; } JSONObject dateObj = JSONObject.fromObject(JSONObject.fromObject(dataExchangeResult).getString("recvdetails")); JSONArray orderArray = JSONArray.fromObject(dateObj.getString("detailline")); if (ObjectUtils.isEmpty(orderArray)){ return "暂无需要获取的订单数据! "; } TempSscApplyGoodsLinesQueryParam tempSscApplyGoodsLinesQueryParam = new TempSscApplyGoodsLinesQueryParam(); tempSscApplyGoodsLinesQueryParam.setApplyId(applyId); tempSscApplyGoodsLinesQueryParam.setOrderSource(platformId); JSONArray insertArray = new JSONArray(); for (Object orderO : orderArray) { JSONObject order = JSONObject.fromObject(orderO); String orderId = order.getString("orderno"); String orderDetailId = order.getString("orderdcd"); tempSscApplyGoodsLinesQueryParam.setOrderId(orderId); tempSscApplyGoodsLinesQueryParam.setDetailOrderId(orderDetailId); TempSscApplyGoodsLinesDto tempSscApplyGoodsLinesDto = tempSscApplyGoodsLinesManager.queryOne(tempSscApplyGoodsLinesQueryParam); if (ObjectUtils.isEmpty(tempSscApplyGoodsLinesDto)) { insertArray.add(orderO); } } if (ObjectUtils.isEmpty(insertArray)){ return "暂无需要获取的订单数据! "; } return jsonArrayToXml(insertArray); } /** * JSON转XML * * @param jsonArray * @return */ public String jsonArrayToXml(JSONArray jsonArray) { String xmlResult = null; Document document = DocumentHelper.createDocument(); Element addElement = document.addElement("ORDERS"); if (jsonArray.size() > 0) { for (int i = 0; i < jsonArray.size(); i++) { JSONObject jsonObject = (JSONObject) jsonArray.get(i); Element childrenElement = addElement.addElement("ORDER"); for (Object key : jsonObject.keySet()) { Element childrenElement2 = childrenElement.addElement(String.valueOf(key).toUpperCase()); childrenElement2.setText(jsonObject.get(key).toString()); } } } xmlResult = document.asXML(); return xmlResult; } }
最新发布
08-28
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值