asp.net中使用JSON

一,.net部分:

1.    [Serializable]
    public class ShopWebInfo
    {
        /// <summary>
        /// 商城商品ID
        /// </summary>
        public int ShopProductID { get; set; }

        /// <summary>
        /// 商品BaseID
        /// </summary>
        public int ProductBaseID { get; set; }

        /// <summary>
        /// 网店ID
        /// </summary>
        public int CompanyID { get; set; }

        /// <summary>
        /// 网店名称
        /// </summary>
        public string CompanyName { get; set; }

        /// <summary>
        /// 销售价格
        /// </summary>
        public decimal SalePrice { get; set; }

        /// <summary>
        /// 赠送积分
        /// </summary>
        public int Score { get; set; }

        /// <summary>
        /// 库存剩余
        /// </summary>
        public int StockNum { get; set; }
    }

2.

[Serializable]
    public class ShopWebBranch
    {
        /// <summary>
        /// 网店ID
        /// </summary>
        public int CompanyID { get; set; }

        /// <summary>
        /// 提货处名称
        /// </summary>
        public string BranchAddress { get; set; }

        /// <summary>
        /// 提货处电话
        /// </summary>
        public string BranchTell { get; set; }
    }

 

3.//序列化成json

IList<ShopWebInfo> lstShopWebInfo = new List<ShopWebInfo>();

IList<ShopWebBranch> lstShopWebBranch = new List<ShopWebBranch>();
            JavaScriptSerializer objSerializer = new JavaScriptSerializer();
            strJson.Append("{");

            //门店列表
            strJson.Append("\"Shops\": [");
            string[] lstShopJson = lstShopWebInfo.Select(i => objSerializer.Serialize(i)).ToArray();
            strJson.Append(string.Join(",",lstShopJson));
            strJson.Append("],");

            //门店提货处
            strJson.Append("\"Branches\": [");
            string[] lstBranchJson = lstShopWebBranch.Select(i => objSerializer.Serialize(i)).ToArray();
            strJson.Append(string.Join(",", lstBranchJson));
            strJson.Append("]");

            strJson.Append("}");
            return strJson.ToString();

 

二 html部分

function GetShopWebInfo(strBaseID) {
    $(".ProductBase").css("display","none");
        $.ajax({
            url: "GetShopWebInfo.ashx?BaseID=" + strBaseID,      //这是包含上述代码的页面
            async: false,
            dataType: "json",
            success: function(strJson)
            {
                var strHtml = "";
                for (var i = 0; i < strJson.Shops.length; i++) {
                    strHtml += "<div class='detail_sj01'>";
                    strHtml += "  <dl>";
                    strHtml += "    <input name='Submit' type='submit' class='button' value='订 购' />";
                    strHtml += "    <dd>" + strJson.Shops[i].CompanyName + "</dd>";
                    strHtml += "    <dd>商品销售价格:¥" + strJson.Shops[i].SalePrice + "</dd>";
                    strHtml += "    <dd>返还积分:" + strJson.Shops[i].Score + "</dd>";
                    strHtml += "    <dd>剩余库存:" + strJson.Shops[i].StockNum + " </dd>";
                    strHtml += "  </dl>";
                    strHtml += GetBranchHTML(strJson.Shops[i].CompanyID, strJson);
                    strHtml += "</div>";
                }
                $("#ProductBase" + strBaseID).html(strHtml);
                $("#ProductBase" + strBaseID).css("display", "block");
            }
        });
    }

    function GetBranchHTML(CompanyID, strJson)
    {
        var strHtml = "";
        strHtml += "  <ul>";
        for (var i = 0; i < strJson.Branches.length; i++)
        {
            if (strJson.Branches[i].CompanyID == CompanyID)
            {
                strHtml += "    <li>提货地:  " + strJson.Branches[i].BranchAddress + ",电话" + strJson.Branches[i].BranchTell + "</li>";
            }
        }
        strHtml += "  </ul>";
        return strHtml;
    }

posted on 2011-12-05 16:22  冰危节奏  阅读(146)  评论(0)    收藏  举报

导航