码迷,mamicode.com
首页 > Web开发 > 详细

json转bean对象

时间:2016-03-18 21:54:18      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:

一下为个人收藏,以便下次使用。

前端传的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对象

标签:

原文地址:http://www.cnblogs.com/1246447850qqcom/p/5293600.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!