springmvc中string字符串中包含多重list数组,转换成bean
import java.util.List;

public class OrderAndDetail {
   private String id;
   private String u_id;
   private String cl_id;
   private String order_time;
   private String pickUp_time;
   private String finish_time;
   private String status;
   private String kind_id;
   private int order_id;
   private String totalprice;
   private int count;
   private List<OrderDetail> orderDetails;

这是model,现在有string传过来,包含了两组List,求通过map解析并获取的方式

4个回答

通过谷歌的gson直接转成bean对象

你问问题,应该要表达清楚,你如果要解析为何不将String字符封装成json数据这样你取值就比较方便

qq_17816517
专注小软件开发 以传
4 年多之前 回复
qq_17816517
专注小软件开发 当时的确这么做过,由于特殊原因总是报错,现在就想把这个字符串放到包含复杂对象的model中,你看我controller里写的,我马上传
4 年多之前 回复
qq_17816517
专注小软件开发 当时的确这么做过,由于特殊原因总是报错,现在就想把这个字符串放到包含复杂对象的model中,你看我controller里写的,我马上传
4 年多之前 回复

```public @ResponseBody String submitOrder(String orderAndDetail, HttpSession session, HttpServletRequest request, HttpServletResponse response,Model model) {
Map classMap = new HashMap();

       JSONObject jsonObject = JSONObject.fromObject(orderAndDetail);
        classMap.put("orderDetails", OrderDetail.class);
       OrderAndDetail orderAndDetail1 = ( OrderAndDetail) JSONObject.toBean(jsonObject,OrderAndDetail.class,classMap);
       OrderDetail orderDetail=new OrderDetail();

Order order=new Order();
order.setAddress(orderAndDetail1.getAddress());
order.setCl_id(orderAndDetail1.getcl_id());
order.setNotes(orderAndDetail1.getnotes());
order.setPickUp_time(orderAndDetail1.getPickUp_time());
order.setTotalprice(orderAndDetail1.getTotalprice());
order.setU_id(u_id);
}


```public @ResponseBody String submitOrder(String orderAndDetail, HttpSession session, HttpServletRequest request, HttpServletResponse response,Model model) {
Map classMap = new HashMap();

       JSONObject jsonObject = JSONObject.fromObject(orderAndDetail);
        classMap.put("orderDetails", OrderDetail.class);
       OrderAndDetail orderAndDetail1 = ( OrderAndDetail) JSONObject.toBean(jsonObject,OrderAndDetail.class,classMap);
       OrderDetail orderDetail=new OrderDetail();

Order order=new Order();
order.setAddress(orderAndDetail1.getAddress());
order.setCl_id(orderAndDetail1.getcl_id());
order.setNotes(orderAndDetail1.getnotes());
order.setPickUp_time(orderAndDetail1.getPickUp_time());
order.setTotalprice(orderAndDetail1.getTotalprice());
order.setU_id(u_id);
}


Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问