常用代碼之四:創建jason,jason轉換為字符串,字符串轉換回jason,c#反序列化jason字符串的幾個代碼片段


1.創建jason,並JSON.stringify()將之轉換為字符串。

直接使用var customer={}, 然后直接customer.屬性就可以直接賦值了。

也可以var customer = { CustomerName: CustomerName, CustomerAddress: CustomerAddress } 這樣創建,它會自動將:前面的CustomerName視作屬性名並加上雙引號,並將后面的CustomerName當作屬性值,讀取變量值后也加上雙引號,當然,這不如上面的方式面向對象。

提交表單前,要使用JSON.stringify()方法將jason對象轉換為字符串。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebAppJason._Default" %>
<head runat="server">
    <title></title>
        <meta http-equiv="X-UA-Compatible" content="IE=9" />
        <script type="text/javascript">
            function abc() {
                var customer = {};
                customer.CustomerName = document.getElementById("CustomerName").value;
                customer.CustomerAddress = document.getElementById("CustomerAddress").value;
                customer = JSON.stringify(customer);
                //alert(customer);
                document.getElementById("customer").value = customer;
            }
    </script>
</head>
<body>
    <form id="form1" runat="server" >
    <div>
        <input type="text" id="CustomerName"  />
        <input type="text" id="CustomerAddress"  />
        <input type="text" id="customer" runat="server" />
        <input type="button" id="button1" value="button1" onclick="abc()"  />
        <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />
        &nbsp;
        <input type="text" id="CustomerName0" runat="server"  />
        <input type="text" id="CustomerAddress0" runat="server" /></div>
    </form>
</body>
</html>

2.在C#中,引用system.web.extension.dll,並using System.Web.Script.Serialization,然后直接用JavaScriptSerializer的Deserialize方法把字符串反序列化為Customer對象使用了,非常簡單方便。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;
namespace WebAppJason
{
    public class Customer { 
       public string CustomerName = "";
       public string CustomerAddress = "";
    }
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string custr = this.customer.Value;
            if (custr != null && custr.Length > 0)
            {
                JavaScriptSerializer jsc = new JavaScriptSerializer();
                Customer c = jsc.Deserialize<Customer>(custr);

                this.CustomerName0.Value = c.CustomerName;
                this.CustomerAddress0.Value = c.CustomerAddress;
            }
        }
    }
}

3.使用JSON.parse()將字符串轉回jason

            function abc() {
                var CustomerName = document.getElementById("CustomerName").value;
                var CustomerAddress = document.getElementById("CustomerAddress").value;
                var customer = {};
                customer.CustomerName = CustomerName;
                customer.CustomerAddress = CustomerAddress;
                customer = JSON.stringify(customer);
                //alert(customer);
                var c2 = JSON.parse(customer);
                alert(c2.CustomerName + " " + c2.CustomerAddress);
                document.getElementById("customer").value = customer;

            }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM