json数据交互

1.Java代码

public void editProduct(HttpServletRequest request,
            HttpServletResponse response, @RequestParam String id)
            throws IOException, JSONException {
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache, must-revalidate");
        response.setHeader("Pragma", "no-cache");
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("productCode", id);
        List<Product> productList = productService.selectlistPageProduct(queryMap, 0, 1);
        if (productList.size() > 0) {
            List<ShopType> mapList = new ArrayList<ShopType>();
            mapList=shoptypeService.ibatorselectShopTypemap();
            /*
             * {
             *  productObj : SendMessageUtil.toJson(productList.get(0))
             *  productType:[SendMessageUtil.toJson(productTypeList.get(0)),SendMessageUtil.toJson(productTypeList.get(1)) ]
             * }
             *
             * */
            org.json.JSONObject productObject = new org.json.JSONObject();
            productObject.put("productObj", SendMessageUtil.toJson(productList.get(0)));
            productObject.put("shopTypeArray", SendMessageUtil.toJson(mapList));
            response.getWriter().write(productObject.toString());
                 
        } else {
            response.getWriter().write(Const.FAIL_STRING);
        }
        response.getWriter().flush();
        response.getWriter().close();
    }

2.页面获取

function editProduct(id){
    $.post("editProduct.do?id="+id,
            function(data) {
                if("fail" == data){
                    alert("【请求失败】");
                }else{
                    var obj = jQuery.parseJSON(data);
                    var product=jQuery.parseJSON(obj.productObj);
                    var shoptype=jQuery.parseJSON(obj.shopTypeArray);//获取到商品类型数组
                    var labers = new Array('商品编号:','商品条码:','商品类型:','商品名:','商品描述:','商品单价:');
                    var texts = new Array(6);
                    texts[0] = '<input type="hidden" id="edit_productCode" name="productCode" value="'+product.productCode+'" />'+
                        '<input type="text" id="edit_productCode_dis" name="productCode_dis" value="'+product.productCode+'" style="border: 0;" class="fisherFilterValueInput" disabled="disabled"/>';
                    texts[1] = '<input type="text" id="edit_productBarCode" name="productBarCode" value="'+product.productBarCode+'" style="border: 0;" class="fisherFilterValueInput"/>';
                    var type_text = '<select id="edit_productType" name="productType" style="border: 0;"class="fisherFilterValueInput">';
                    //if(obj.sex == 1){
                        //type_text += '<option value="1" selected="selected">普通商户</option><option value="2" >vip商户</option>';
                    //}else{
                        //type_text += '<option value="1">普通商户</option><option value="2" selected="selected">vip商户</option>';
                    //}
                    for(var i=0;i<shoptype.length;i++){
                        if(product.productType==shoptype[i].id){
                            type_text += '<option value="'+shoptype[i].id+'" selected="selected">'+shoptype[i].shoptype+'</option>';
                        }else{
                            type_text += '<option value="'+shoptype[i].id+'">'+shoptype[i].shoptype+'</option>';
                        }
                    }


你可能感兴趣的:(request)