创建jason,jason转换为字符串,字符串转换回jason,c#反序列化jason字符串的几个代码片段
来源:互联网 发布:股票操盘手训练软件 编辑:程序博客网 时间:2024/05/17 06:22
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" /> <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; }
0 0
- 创建jason,jason转换为字符串,字符串转换回jason,c#反序列化jason字符串的几个代码片段
- jason与字符串的转换
- java解析jason字符串
- Jason
- 把jason字符串转化为标注的data(responseObject)类型
- jason jason jason
- Jason序列化与反序列化
- Retrofit 标准版post请求 提交jason字符串
- jason与array之间的相互转换
- 【Jason's_Knowledge】算法竞赛中的字符与字符串
- Jason解析的实现
- jason数据格式
- android jason
- About Jason.
- Jason Suz
- Jason Koo
- jason数据格式
- 转jason
- ScrollView起始位置不是最顶部的解决办法
- Git使用优秀总结
- 栈和堆之间有什么区别?
- Day6、Python
- (好)C++ 对象的内存布局(上)
- 创建jason,jason转换为字符串,字符串转换回jason,c#反序列化jason字符串的几个代码片段
- 去哪儿编程题-寻找Coder
- Thrift安装使用教程
- 数字图像处理之灰度图的0是黑还是白,255是黑还是白?
- CentOS 6.5 升级gcc-4.9.2
- 设计模式 原型模式 Prototype
- 如何使用命令行重新设置SharePoint Central Administration?
- S5PV210的中断体系介绍
- ios企业版证书怎样配置plist文件里边的url