c/c++常用代码之四爆炸输出,jason(纯干货)


常用代码之四:创建jason,jason转换为字符串,字符串转换回jason,c#反序列化jason字符串的几个代码片段

创建jason,并JSON.stringify()将之转换为字符串。

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

也可以var customer = { CustomerName: CustomerName, CustomerAddress: CustomerAddress } 这样创建,它会自动将:前面的CustomerName视作属性名并加上双引号,并将后面的CustomerName当作属性值,读取变量值后也加上双引号,当然,这不如上面的方式面向对象。

1240

 

 

提交表单前,要使用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" οnclick="abc()"  />

        <asp:Button ID="Button2" runat="server" οnclick="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;

            }

文末也给大家,分享主要有C/C++,Linux,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,TCP/IP,协程,DPDK技术,面试技巧方面的资料技术讨论。

感兴趣的朋友可以加群:812855908

 

出处:http://www.cnblogs.com/liuzhendong

转载于:https://my.oschina.net/u/3997182/blog/3080465

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值