JSON使用方法

关于Struts2 spring + hibernate进Q群: 130529143交流。
有偿技术支持Q群:
398162181 



1.在后台将数据封装到json中:

List<Product> productList = productService.getProductList(1,typeId);
        StringBuffer stringb = new StringBuffer("[");
        for(int i = 0; i < productList.size(); i++)
        {
            stringb.append("{");
            stringb.append("id:\"" + productList.get(i).getId() + "\"," );
            stringb.append("typeId:\"" + productList.get(i).getTypeId() + "\"," );
            stringb.append("name:\"" + productList.get(i).getName() + "\"," );
            stringb.append("presentation:\"" + productList.get(i).getPresentation() + "\"," );
            stringb.append("path:\"" + productList.get(i).getPath() + "\"," );
            stringb.append("status:\"" + productList.get(i).getStatus() + "\"" );
            stringb.append("}");
            
            if(i < productList.size() - 1)
            {
                stringb.append(",");
            }
        }
        
        stringb.append("]");


        try
        {
        //将封装好的json格式的字符串传到前台页面    

getResponse().getWriter().print(stringb.toString());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }


2.在前台页面通过js使用json格式的字符串:

/*
flag="delete":删除productId的产品
flag="add":新增typeId类的产品
flag="select":查询typeId的所有产品
*/
function showProductList(flag, productId, typeId){
//为全局mainTypeId赋值
mainTypeId = "typeList" + typeId;
var proName = "";
if(flag == "add"){
proName = $("#proName").val();
if(proName == ""){
alert("请输入需添加的产品名称 !");
return false;
}

$.ajax({
type : "POST",
url : "manage_product_list.action",
data : "flag=" + flag + "&typeId=" + typeId + "&proId=" + productId + "&proName=" + proName,
dataType : "text",
success : function(data) {
//alert(data);
//将字符串转为JSON对象
var obj = eval('(' + data + ')');   
var length = obj.length;

//拼html
var _html = "<ul>";
for(var i = 0; i < length; i++){
  _html += "<li class='productList link' id=\"" + obj[i].id+ "\">" + (i+1) + "." +obj[i].name + "</li>" + 
           "<li class='productDelBtn link' οnclick='showProductList(" + "\"delete\"," +obj[i].id + "," + obj[i].typeId+ ");'>✘</li>" ;
}
_html += "<li id='addProductList'><input id='proName' type='text'></li>" + 
         "<li class='productDelBtn link' οnclick='showProductList(" + "\"add\"," +  "0," + typeId + ");'>✔</li>"
_html += "</ul>";

//将_html加入div
$("#list").children("div").html(_html);

$(".productList").each(function(){
   $(this).click(function(){
    $("#content").css("display", "block");
$("#list").css("display", "none");
//id=product_img的iframe的src赋值
$("#product_img").attr("src","manage_product_img.action?productId=" + $(this).attr("id"));
    var productId = $(this).attr("id");
    $("#productId").attr("value",productId);
       for(var i = 0; i < length; i++){
        var id = obj[i].id;
        if(productId == id){
        var name = obj[i].name;
var presentation = obj[i].presentation;
var status = obj[i].status;
//alert(status);
$("#productType option[value = " + typeId + "]").attr("selected",true);
$("#productName").val(name);
$("#content input[type = 'radio'][value=" + status + "]").attr("checked",true);
$("#presentation").val(presentation);
        }
       }
   });
   });
}
});
}

关于Struts2 spring + hibernate进Q群: 130529143交流。
有偿技术支持Q群:
398162181 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值