json转bean对象
Posted 小安妮的熊
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了json转bean对象相关的知识,希望对你有一定的参考价值。
一下为个人收藏,以便下次使用。
前端传的json格式为:
[{"suppliercode":"gylhld_gycqlt3_gycqlt1","productname":"乐和乐都套票(成人)","datahandelmethod":"1","producttype":"1","yscpcbj":"140","effectivetime":"2016-01-27 00:00:00","invalidtime":"2016-02-29 00:00:00","status":"1","id":"7066cb7c-fd52-4949-b7a1-776812995023","organid":"","summaryurl":"","createtime":"2016-01-29 16:25:46","createuserid":"0b8d60c9-c3b6-498f-a7f2-a28b7bf99232","createuser":"兰洋","updateuserid":"0b8d60c9-c3b6-498f-a7f2-a28b7bf99232","updatetime":"2016-01-29 16:25:46","updateuser":"兰洋"}]
我们最终的目的是将json传转为对象
如下获取一个params参数:
HttpServletRequest request = getContext().getRequest(); String params = request.getParameter("params"); JSONArray array = JSONArray.fromObject(params);//先读取串数组 Object[] o = array.toArray();//转成对像数组 JSONObject obj = JSONObject.fromObject(o[0]);//再使用JsonObject遍历一个个的对像 Shopgoodsprductys shopgoodsprductys = (Shopgoodsprductys)obj.toBean(obj,Shopgoodsprductys.class);//指定转换的类型,但仍需要强制转化-成功
前端传参数格式例如:
var shopgoodsprductys = []; shopgoodsprductys.push({ "suppliercode":suppliercode, "productname":productname, "datahandelmethod":datahandelmethod, "producttype":producttype, "yscpcbj":yscpcbj, "effectivetime":effectivetime, "invalidtime":invalidtime, "status":status, "id":id ........ });
var params = JSON.stringify(shopgoodsprductys); $.ajax({ type: ‘POST‘, data: {params:params}, url: getbasePathUrl(‘zxxt/Shopgoodsprductys.action?saveShopgoodsprductys=‘), dataType:"json", success: function (result) { alert("操作成功"); }, error: function (xhr) { alert("操作失败"); } });
以上是关于json转bean对象的主要内容,如果未能解决你的问题,请参考以下文章