Json序列化和反序列化方法解析_jquery
///
/// Json序列化,用于发送到客户端
///
public static string ToJsJson(this object item)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(item.GetType());
using (MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, item);
StringBuilder sb = new StringBuilder();
sb.Append(Encoding.UTF8.GetString(ms.ToArray()));
return sb.ToString();
}
}
///
/// Json反序列化,用于接收客户端Json后生成对应的对象
///
public static T FromJsonTo
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
T jsonObject = (T)ser.ReadObject(ms);
ms.Close();
return jsonObject;
}
实体类
[DataContract]
public class TestObj
{
[DataMember]
public string make { get; set; }
[DataMember]
public string model { get; set; }
[DataMember]
public int year { get; set; }
[DataMember]
public string color { get; set; }
}
------------------javascript获取Json--------------------
javascript调用测试代码
$('#getJson').click(function() {
$.ajax({
url: "getJsonHandler.ashx",
type: 'GET',
data: {},
dataType: 'json',
timeout: 1000,
error: function(XMLHttpRequest, textStatus, errorThrown) { alert(textStatus) },
success: function(result) {
alert(result.make);
alert(result.model);
alert(result.year);
alert(result.color);
}
});
});
C#后台生成代码
public class getJsonHandler: IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
TestObj obj = new TestObj();
obj.make = "Make is Value";
obj.model = "Model is Value";
obj.year = 999;
obj.color = "Color is Value";
context.Response.Write(obj.ToJsJson());
}
public bool IsReusable
{
get
{
return false;
}
}
}
//返回值为 {"color":"Color is Value","make":"Make is Value","model":"Model is Value","year":999}
-----------------C#由Json生成对象-----------------------
javascript调用测试代码
$('#postJson').click(function() {
var m_obj = { make: "Dodge", model: "Coronet R/T", year: 1968, color: "yellow" };
var jsonStr = JSON.stringify(m_obj); //用Json2.js生成Json字符串
$.ajax({
url: "postJsonHandler.ashx",
type: 'POST',
data: { postjson: jsonStr },
dataType: 'json',
timeout: 1000,
error: function(XMLHttpRequest, textStatus, errorThrown) { alert(textStatus) },
success: function(result) {
alert(result.success);
}
});
});
C#后台生成代码
public class postJsonHandler: IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string jsonStr = context.Request["postjson"];
TestObj obj = jsonStr.FromJsonTo
if (string.IsNullOrEmpty(obj.make) || string.IsNullOrEmpty(obj.model) || string.IsNullOrEmpty(obj.color)
|| obj.year {
context.Response.Write("{success:false}");
}
else
{
context.Response.Write("{success:true}");
}
public bool IsReusable
{
get
{
return false;
}
}
}
使用Json时需要注意,服务器端拼凑生成Json字符串时,一定要注意把字符串用\"\"包裹,不然客户端接收时肯定会报错,根据Json字符串生成对象,是根据对应的名称赋值,多于或少于都不会报错.
推荐阅读
-
jquery遍历筛选数组的几种方法和遍历解析json对象
-
jQuery序列化form表单数据为JSON对象的实现方法
-
ASPNET中JSON的序列化和反序列化的方法
-
JSON键值对序列化和反序列化解析
-
Newtonsoft.Json C# Json序列化和反序列化工具的使用、类型方法大全
-
C# .NET的BinaryFormatter、protobuf-net、Newtonsoft.Json以及自己写的序列化方法序列化效率和序列化后的文件体积大小对比
-
jQuery序列化form表单数据为JSON对象的实现方法
-
JavaScript解析及序列化JSON的方法实例分析
-
Json序列化和反序列化方法解析
-
JSON键值对序列化和反序列化解析