.Net 下开发使用JSON
var obj = new Object();
obj.Name = "老杨";
obj.Title = "CIO";
var x = JSON.stringify(obj);//json2.js文件中提供的方法
$.post("ServerOrResource/TE.ashx", {"ob":x}, function (res) {
var hs = "姓名:" + res.Name + " 职务:" + res.Title; //可以直接通过属性名称来进行访问。
$("#divA").html(hs);
}, "json");
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization.Json;
///JsonHelper 的摘要说明
/// </summary>
public class JsonHelper
{
public static string SerializeObject(Object obj)
{
string json = "";
DataContractJsonSerializer ser = new DataContractJsonSerializer(obj.GetType());
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
ser.WriteObject(ms, obj);
json = System.Text.Encoding.UTF8.GetString(ms.ToArray());
}
return json;
}
public static T DeSerializeObject<T>(string json)
{
System.Text.UTF8Encoding utf = new System.Text.UTF8Encoding();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
using (System.IO.MemoryStream s = new System.IO.MemoryStream(utf.GetBytes(json)))
{
return (T)ser.ReadObject(s);
}
}
}
{
context.Response.ContentType = "text/plain";
string json = context.Request.Form["ob"];
Employee emp = JsonHelper.DeSerializeObject<Employee>(json);
string ret = JsonHelper.SerializeObject(emp);
context.Response.Write(ret);
}
public class Employee
{
string name = "";
public string Name
{
get { return name; }
set { name = value; }
}
string title = "";
public string Title
{
get { return title; }
set { title = value; }
}
}
public class Employee
{
string name = "";
[System.Runtime.Serialization.DataMember]
public string Name
{
get { return name; }
set { name = value; }
}
string title = "";
[System.Runtime.Serialization.DataMember]
public string Title
{
get { return title; }
set { title = value; }
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。