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 > )
{
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; }

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

  1. H面试程序(28):字符串处理转换

    //2 字符串处理转换 //问题描述: //在给定字符串中找出单词( “单词”由大写字母和小写字母字符构成, //其他非字母字符视为单词的间隔,如空格.问号.数字等等:另外单个字母不算单词): //找 ...

  2. UDP代码编写、操作系统发展史、多道技术、进程理论与代码层面创建、进程join方法与进程对象方法

    昨日内容回顾 socket基本使用 # 内置的模块 import socket s = socket.socket() # 默认是TCP协议 也可以切换为UDP协议 s.bind((ip,port)) ...

  3. 【MySQL】MySQL中针对大数据量常用技术_创建索引+缓存配置+分库分表+子查询优化(转载)

    原文地址:http://blog.csdn.net/zwan0518/article/details/11972853 目录(?)[-] 一查询优化 1创建索引 2缓存的配置 3slow_query_ ...

  4. JAVA将数字字符串强制转换成整型变量----求参数之和实验代码(附流程图)

    一.设计思想 先将参数个数输出,并利用循环结果将参数逐个输出,再将字符串强制转化成整型,利用循环结构相加求和 二.程序流程图 三.源程序代码 package demo; public class Co ...

  5. VS中如何快捷地给自己的代码添加创建信息注释

    VS中如何快捷地给自己的代码添加创建信息注释 Intro 以下讨论的都是没有使用 GIT 来管理源代码的情况,如果使用 GIT 管理源代码可直接使用VS的Git扩展就不需要考虑以下问题. 什么是创建信 ...

  6. 在代码中创建Drawable资源

    如何在代码中创建圆环: 先看效果图 代码; import android.graphics.drawable.GradientDrawable; GradientDrawable circle = n ...

  7. 分享非常有用的Java程序 (关键代码) (三)---创建ZIP和JAR文件

    原文:分享非常有用的Java程序 (关键代码) (三)---创建ZIP和JAR文件 import java.util.zip.*; import java.io.*; public class Zip ...

  8. 字符串json转换为xml xml转换json

    原文:字符串json转换为xml xml转换json // To convert an XML node contained in string xml into a JSON string XmlD ...

  9. JAVA字符串编码转换常用类

    无论是对程序的本地化还是国际化,都会涉及到字符编码的转换的问题.尤其在web应用中常常需要处理中文字符,这时就需要进行字符串的编码转换,将字符串编码转换为GBK或者GB2312.一.关键技术点:    ...

随机推荐

  1. openfiler在esxi下的安装配置

    注意分区的时候如果硬盘太小自动分区会导致分配的卷大小不够用 后改为如下: 以root登录: 应该以openfiler登录,口令是password 也可以导入虚拟机安装 升级虚拟机硬件版本 终端登录用户 ...

  2. 1067: 密室寻宝(find)

    #include <iostream> #include <iomanip> #include <cstdlib> #include <string> ...

  3. Hello World程序演示Qt项目的建立、编译、运行和发布

    一.Qt项目的建立 1.选择项目模板. 选择"文件-->新建文件或工程",在选择模板页面选择Qt C++项目中的"Qt Gui应用"一项,然后单击&quo ...

  4. tar 使用总结

    今天需要备份数据,使用tar命令,总结一下. 压缩命令: tar -zvvf ticket-data-intgration.tar.gz ticket-data-intgration 压缩但是不包含某 ...

  5. javascript将算法复杂度从O(n^2)做到O(n)

    compare the difference of two giving array, return results: 1. elements in both array, 2. elements o ...

  6. MySQL 主从错误

    1: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log fil ...

  7. 玩转Bootstrap

    一:bootstrap基本模版 <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

  8. Shell编程初步

      一:Hello World 新建一个文件,命名时以 .sh 为后缀.每个bash文件开头第一行表名文件类型: #!/bin/bash 然后在下面输入代码. 比如输出hello world: #!/ ...

  9. JavaWeb应用出现HTTP 500-Unable to compile class for JSP 错误 的解决

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6383192.html 在上一篇博文中,我们把自己本机的web项目部署到了云主机的tomcat上.之后通过浏览器 ...

  10. 〖Android〗ant build android project, setting android.jar precedence

    最近公司的一个项目中,新增了classes.jar包,与android.jar有冲突,必须得在加载android.jar前行加载classes.jar: 在持续集成环境中须使用ant编译,classe ...