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. 008-Go 关于字符串拼接

    如果是少量小文本拼接,用 “+” 如果是大量小文本拼接,用 strings.Join 如果是大量大文本拼接,用 bytes.Buffer package main import( "fmt& ...

  2. MySQL 主从错误

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

  3. Aerospike系列:4:简单的增删改查aql

    [root@localhost bin]# aql --help Usage: aql OPTIONS OPTIONS -h <host> The hostname to the serv ...

  4. 《Unix&Linux大学教程》学习笔记二:指令常识

    1:指令的本质——调用可执行程序 在Shell输入命令时,其实就是根据名称运行相应的程序. 2:在系统中查找程序 which 指令名 type 指令名 3:时间与日历 date [-选项] 参数 ca ...

  5. BIEE启动关闭服务(转)

    一.环境说明 版本:BIEE11g (BIEE_11.1.1.9.0) OS:CentOS 6.5 64bit (所有的linux服务器都适用) 二.BIEE启动与关闭 BIEE11g 的启动包括三个 ...

  6. LeetCode222 Count CompleteTree Nodes(计算全然二叉树的节点数) Java 题解

    题目: Given a complete binary tree, count the number of nodes. Definition of a complete binary tree fr ...

  7. jqueryMobile 动态添加元素,展示刷新视图方法

    jqueryMobile动态添加元素jqueryMobile郏高阳 jQuery Mobile的是一个很好的移动开发框架,你可能已经知道,虽然它有很多难以解决的问题,但是我相信后续版本jquery会修 ...

  8. NoSQL生态系统(nosql ecosystem)

    Unlike most of the other projects in this book, NoSQL is not a tool, but an ecosystem composed of se ...

  9. Sql Server 在数据库中所有表所有栏位 找出匹配某个值的脚本(转)

    转自: http://blog.csdn.net/chenghaibing2008/article/details/11891419 (下面代码稍有修改,将要查找的内容直接作为参数传人,并且使用=而不 ...

  10. 【DM】Combating Web Spam with TrustRank - 用TrustRank对抗网络垃圾邮件

    [论文标题]Combating Web Spam with TrustRank (Proceedings 2004 VLDB Conference) [论文作者]Zolt´an Gy¨ongyi,He ...