Json序列化和反序列化方法解析
/// <summary>
/// Json序列化,用于發(fā)送到客戶端
/// </summary>
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();
}
}
/// <summary>
/// Json反序列化,用于接收客戶端Json后生成對應的對象
/// </summary>
public static T FromJsonTo<T>(this string jsonString)
{
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調(diào)用測試代碼
$('#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調(diào)用測試代碼
$('#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<TestObj>();
if (string.IsNullOrEmpty(obj.make) || string.IsNullOrEmpty(obj.model) || string.IsNullOrEmpty(obj.color)
|| obj.year < 0)
{
context.Response.Write("{success:false}");
}
else
{
context.Response.Write("{success:true}");
}
public bool IsReusable
{
get
{
return false;
}
}
}
使用Json時需要注意,服務器端拼湊生成Json字符串時,一定要注意把字符串用\"\"包裹,不然客戶端接收時肯定會報錯,根據(jù)Json字符串生成對象,是根據(jù)對應的名稱賦值,多于或少于都不會報錯.
- JSON PHP中,Json字符串反序列化成對象/數(shù)組的方法
- PHP中SERIALIZE和JSON的序列化與反序列化操作區(qū)別分析
- PHP的serialize序列化數(shù)據(jù)以及JSON格式化數(shù)據(jù)分析
- php json與xml序列化/反序列化
- php 中序列化和json使用介紹
- php中serialize序列化與json性能測試的示例分析
- js解析與序列化json數(shù)據(jù)(一)json.stringify()的基本用法
- jquery序列化form表單使用ajax提交后處理返回的json數(shù)據(jù)
- js解析與序列化json數(shù)據(jù)(二)序列化探討
- php中json?序列化為?[]?的弊端
相關文章
jquery將json轉(zhuǎn)為數(shù)據(jù)字典的實例代碼
這篇文章主要介紹了jquery將json轉(zhuǎn)為數(shù)據(jù)字典的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10jquery text(),val(),html()方法區(qū)別總結
jquery text(),val(),html()方法區(qū)別總結。需要的朋友可以過來參考下,希望對大家有所幫助2013-11-11jquery對象和javascript對象即DOM對象相互轉(zhuǎn)換
對于已經(jīng)是一個 DOM 對象,只需要用 $() 把DOM對象包裝起來,就可以獲得一個 jQuery 對象了,使用[index]和.get(index)可以轉(zhuǎn)為DOM對象2014-08-08基于Jquery和CSS3制作數(shù)字時鐘附源碼下載(CSS3篇)
數(shù)字時鐘在web倒計時,web鬧鐘效果以及基于html5的web app中,本文給大家介紹基于jquery和css3制作數(shù)字時鐘附源碼下載,感興趣的朋友來看看吧2015-11-11