Map集合使用生成Json

   @PostMapping("/takeUserOrder")
    public String takeUserOrder(String useOrder,String orderNo){
        String json = orderService.getUserOrder(useOrder);
        //根据订单号获取List的Map集合
        /**
         *     <select id="getItemMap" resultType="java.util.Map">
         *         select catalog_no,cas_no,remarks,quantity,size,amount from user_order_item where order_no=#{order_no}
         *     </select>
         */
        List<Map<String, String>> itemMap = orderService.getItemMap(orderNo);
        //将Json转为Map对象  JSONObject 方法类 put set 等等
        JSONObject jsonObject =JSONObject.parseObject(json);
        //实例化集合
        List<Map<String,Object>> list = new ArrayList<>();
        //put对象
        jsonObject.put("applySaleNo",orderNo);
        //遍历
        itemMap.forEach(p->{
            Map<String,Object> map = new HashMap<>();
            map.put("catalogNo",p.get("catalog_no"));
            map.put("cas",p.get("cas_no"));
            map.put("remarks",p.get("remarks"));
            map.put("saleCount",p.get("quantity"));
            map.put("saleUnit","瓶");
            map.put("spec",p.get("size"));
            map.put("moneyWithTax",0);
            map.put("unitPrice",p.get("amount"));
            map.put("productRemarks","");
            list.add(map);
        });
        //继续塞入
        jsonObject.put("details",list);
        //返回json字符串
        return jsonObject.toString();
    }

示例Json:

{
	"applySaleNo": "xxx",
	"applySourceName": "xxx",
	"ccompanyName": "xxx",
	"creceiverName": "xxx",
	"creceiverPhone": "xxx",
	"creceiverAddr": "xxx",
	"creceiverEmail": "xxx",
	"cbuyerName": "xxx",
	"cbuyerPhone": "xxx",
	"cbuyerEmail": "xxx",
	"cticketPersonName": "xxx",
	"cticketAddr": "xxx",
	"cticketEmail": "xxx",
	"cticketPhone": "xxx",
	"remarks": "xxx",
	"otherInfo": "xxx",
	"sumPrice": xxx,
	"details": [{
		"catalogNo": "xxx",
		"cas": "1703-46-4",
		"remarks": "",
		"saleCount": 1,
		"saleUnit": "瓶",
		"spec": "5g",
		"moneyWithTax": 0,
		"unitPrice": 1120.00,
		"productRemarks": ""
	}],
	"websitePerSonID": 7713,
	"websiteGroupID": 0,
	"websiteCustomerName": "xxx",
	"paymentMethod": "支付"
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值