.net处理JSON简明教程Json.Net是.net中的一种流行的高性能的JSON框架。 特点
序列化方法 private string JsonSerialize(employee emp) { return Newtonsoft.Json.JsonConvert.SerializeObject(emp); } 反序列化方法 private employee JsonDeserialize(string json) { return (employee)(Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(employee))); }
private string JsonSerialize (employee emp) { return new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(emp); } 反序列化 Private string JsonDeserialize (string json){ return (employee)(new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(json,typeof(employee))); }
private string JsonSerialize (employee emp) { System.Runtime.Serialization.Json.DataContractJsonSerializer dcjs = newSystem.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(employee)); System.IO.MemoryStream ms = new System.IO.MemoryStream(); dcjs.WriteObject(ms, emp); string json = System.Text.Encoding.UTF8.GetString(ms.ToArray()); ms.Close(); return json; } 反序列化方法 private employee JsonDeserialize (string json) { System.Runtime.Serialization.Json.DataContractJsonSerializer dcjs = newSystem.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(employee)); System.IO.MemoryStream ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(json)); employee obj = (employee)(dcjs.ReadObject(ms)); return obj; }
private void LinqJson(string json) { JObject obj = Newtonsoft.Json.Linq.JObject.Parse(json); //获取全部数据值 foreach (JToken token in obj.Values()) { Response.Write(token.ToString()); } //获取唯一值 Response.Write(obj["eid"].ToString()+"
string json = @"{ ""Name"": ""Apple"", ""Expiry"": new Date(1230422400000), ""Price"": 3.99, ""Sizes"": [ ""Small"", ""Medium"", ""Large"" ] }"; JObject o = JObject.Parse(json); string name = (string)o["Name"]; // Apple JArray sizes = (JArray)o["Sizes"]; string smallest = (string)sizes[0];
JSON序列化和反序列化日期时间的处理 JSON格式不直接支持日期和时间。DateTime值值显示为"/Date(700000+0500)/"形式的JSON字符串,其中第一个数字(在提 供的示例中为 700000)是 GMT 时区中自 1970 年 1 月 1 日午夜以来按正常时间(非夏令时)经过的毫秒数。该数字可以是负数,以表示之前的时间。示例中包括"+0500"的部分可选,它指示该时间属于Local 类型,即它在反序列化时应转换为本地时区。如果没有该部分,则会将时间反序列化为Utc。 使用DataContractJsonSerializer的序列化方式对日期格式进行处理,其他的(反)序列化方案的使用方式是相同的。设计思想是通过正则表达式匹配,替换JSON里面的日期数据为正常的日期格式。 序列化方法 private string JsonSerialize (employee emp) { System.Runtime.Serialization.Json.DataContractJsonSerializer dcjs = newSystem.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(employee)); System.IO.MemoryStream ms = new System.IO.MemoryStream(); dcjs.WriteObject(ms, emp); string json = System.Text.Encoding.UTF8.GetString(ms.ToArray()); ms.Close(); //替换JSON时间为字符串 string p = @"\\/Date\((\d+)\)\\/"; System.Text.RegularExpressions.MatchEvaluator me = new System.Text.RegularExpressions.MatchEvaluator(ConvertJsonDateToDataString); System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(p); json = regex.Replace(json, me); return json; }
private string ConvertJsonDateToDataString(System.Text.RegularExpressions.Match m) { string result = string.Empty; DateTime dt = new DateTime(1970,1,1); dt = dt.AddMilliseconds(long.Parse(m.Groups[1].Value)); dt = dt.ToLocalTime(); result = dt.ToString("yyyy-MM-dd HH:mm:ss"); return result; }
反序列化方法 private employee JsonDeserialize (string json) { string p = @"\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}"; System.Text.RegularExpressions.MatchEvaluator me = new System.Text.RegularExpressions.MatchEvaluator(ConvertDateStringToJsonDate); System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(p); json = reg.Replace(json, me);
System.Runtime.Serialization.Json.DataContractJsonSerializer dcjs=newSystem.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(employee)); System.IO.MemoryStream ms=new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(json)); employee emp=(employee)dcjs.ReadObject(ms); return emp; }
private string ConvertDateStringToJsonDate(System.Text.RegularExpressions.Match m) { string result = string.Empty; DateTime dt = DateTime.Parse(m.Groups[0].Value); dt = dt.ToUniversalTime(); TimeSpan ts = dt - DateTime.Parse("1970-1-1"); result = string.Format("\\/Date({0}+0800)\\/", ts.TotalMilliseconds); return result; } ========================================================================== using System; using System.Data; namespace WebApplication1 public void ProcessRequest(HttpContext context) NorthwindDataContext db = new NorthwindDataContext(); JsonSerializerSettings settings = new JsonSerializerSettings(); } public bool IsReusable 在页面中使用jquery调用的方式如下
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="jqueryGetOrdersbyajax.aspx.cs" Inherits="WebApplication1.jqueryGetOrdersbyajax" %>
<% = minjquery %> value=" < " /> value=" >> " />
LOADING.... ========================== |