在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
http://www.cnblogs.com/ambar/archive/2010/07/13/parse-json-via-csharp.html
对比
准备数据实体类: [DataContract] public class Person { [DataMember(Order = 0, IsRequired = true)] public string Name { get; set; } [DataMember(Order = 1)] public int Age { get; set; } [DataMember(Order = 2)] public bool Alive { get; set; } [DataMember(Order = 3)] public string[] FavoriteFilms { get; set; } [DataMember(Order = 4)] public Person Child { get; set; } } 定义: Action<object> log = o => Console.WriteLine(o); Func<int, int, int> add = (x, y) => x + y; var p1 = new Person { Age = 12, Alive = true, Name = "lj", FavoriteFilms = new[] { "Up", "Avatar" } }; var p2 = new Person() { Age = 28, Name = "cy", Child = p1 }; 使用DataContractJsonSerializer帮助类: // using System.Runtime.Serialization.Json; /// <summary> /// 解析JSON,仿Javascript风格 /// </summary> public static class JSON { public static T parse<T>(string jsonString) { using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString))) { return (T)new DataContractJsonSerializer(typeof(T)).ReadObject(ms); } } public static string stringify(object jsonObject) { using (var ms = new MemoryStream()) { new DataContractJsonSerializer(jsonObject.GetType()).WriteObject(ms, jsonObject); return Encoding.UTF8.GetString(ms.ToArray()); } } } 用法: // 序列化 var jsonString = JSON.stringify(new[] { p1, p2 }); log(jsonString == JSON.stringify(new List<Person>() { p1, p2 })); //true log(jsonString); // 反序列化,泛型集合 JSON.parse<List<Person>>(jsonString); // 数组转换 JSON.parse<Person[]>(jsonString); 输出: [{"Name":"lj","Age":12,"Alive":true,"FavoriteFilms":["Up","Avatar"],"Child":null },{"Name":"cy","Age":28,"Alive":false,"FavoriteFilms":null,"Child":{"Name":"lj", "Age":12,"Alive":true,"FavoriteFilms":["Up","Avatar"],"Child":null}}] 使用JavaScriptSerializer// using System.Web.Script.Serialization; var jser = new JavaScriptSerializer(); var json = jser.Serialize(new List<Person>() { p1, p2 }); var persons = jser.Deserialize<List<Person>>(json);
使用JSON.NET// using Newtonsoft.Json; var json = JsonConvert.SerializeObject(new[] { p1, p2 }); var persons = JsonConvert.DeserializeObject<List<Person>>(json); var ja = JArray.Parse(jsonString); log(ja); //注意,格式化过的输出 输出: [ { "Name": "lj", "Age": 12, "Alive": true, "FavoriteFilms": [ "Up", "Avatar" ], "Child": null }, { "Name": "cy", "Age": 28, "Alive": false, "FavoriteFilms": null, "Child": { "Name": "lj", "Age": 12, "Alive": true, "FavoriteFilms": [ "Up", "Avatar" ], "Child": null } } ] LINQ: var ageCount = ja.Select(j => (int)j["Age"]).Aggregate(add); var q = from j in ja where !j["Name"].Value<string>().Equals("lj") select (int)j["Age"]; log(q.Aggregate(add) == ageCount); //false 其他: // 与Linq to XML 相似的嵌套构造函数: var jo = new JObject( new JProperty("age", persons.Select( p => p.Age)), new JProperty("funny", true), new JProperty("array", new JArray(new[] { 2, 4, 1 })) ); log(jo); // JObject 操作 var css = "{ \"#header\" : {background:\"red\"}, layout : [5,4,1] }"; var style = JObject.Parse(css); var bd = new JObject(); bd["color"] = "1px solid cyan"; style["border"] = bd; var hd = style["#header"]; style["body>div+p"] = hd; hd.Parent.Remove(); style["layout"][0] = 22; log(style); 输出: { "age": [ 12, 28 ], "funny": true, "array": [ 2, 4, 1 ] } { "layout": [ 22, 4, 1 ], "border": { "color": "1px solid cyan" }, "body>div+p": { "background": "red" } } 几个链接:
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论