有朋友问起,暂记一下,有空好好补充 有好多方法,如果是C#3.0,建议采用内置的方案: 举一个开心网的例子: //--------json/test/userdata------- string strUserData = ContentHelper.GetMidString(parkingHTML, "v_userdata = ", ";"); jsr = new DataContractJsonSerializer(typeof(UserData)); ms = new MemoryStream(Encoding.UTF8.GetBytes(strUserData)); UserData userData = (UserData)jsr.ReadObject(ms); [DataContract] public class Config { [DataMember(Name = "money_minute")] public string money_minute { get; set; } [DataMember(Name = "money_max")] public string money_max { get; set; } } //vuid //uid //neighbor //isfriend //first_fee_parking //first_free_parking //real_name //ta //logo50 //logo20 //avenue //cash //scene //sceneid //showmoneyminute //online2 [DataContract] public class User { [DataMember(Name = "vuid")] public string vuid { get; set; } [DataMember(Name = "uid")] public string uid { get; set; } [DataMember(Name = "neighbor")] public string neighbor { get; set; } [DataMember(Name = "isfriend")] public string isfriend { get; set; } [DataMember(Name = "first_fee_parking")] public string first_fee_parking { get; set; } [DataMember(Name = "first_free_parking")] public string first_free_parking { get; set; } [DataMember(Name = "real_name")] public string real_name { get; set; } [DataMember(Name = "ta")] public string ta { get; set; } [DataMember(Name = "logo50")] public string logo50 { get; set; } [DataMember(Name = "logo20")] public string logo20 { get; set; } [DataMember(Name = "avenue")] public string avenue { get; set; } [DataMember(Name = "cash")] public string cash { get; set; } [DataMember(Name = "scene")] public string scene { get; set; } [DataMember(Name = "sceneid")] public string sceneid { get; set; } [DataMember(Name = "showmoneyminute")] public string showmoneyminute { get; set; } [DataMember(Name = "online2")] public string online2 { get; set; } } 通过JSON序列化深度克隆类型 //空接口 public interface IJson { } public static class JsonHelper { public static string ToJson(this IJson source) { return ToJson(source, source.GetType()); } public static string ToJson(this IJson source, Type type) { DataContractJsonSerializer serilializer = new DataContractJsonSerializer(type); using (Stream stream = new MemoryStream()) { serilializer.WriteObject(stream, source); stream.Flush(); stream.Position = 0; StreamReader reader = new StreamReader(stream); return reader.ReadToEnd(); } } public static T ParseJSON<T>(this string str) { using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(str))) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T)); return (T)serializer.ReadObject(ms); } } // 通过JSON序列化深度克隆类型 public static T DeepClone<T>(this IJson Source) where T : IJson { string jsonString = Source.ToJson(); return jsonString.ParseJSON<T>(); } }