c# 解析JSON的几种办法
程序员文章站
2022-04-28 11:51:28
对比 准备数据 实体类: 定义: 使用DataContractJsonSerializer 帮助类: 用法: 输出: 使用JavaScriptSerializer 使用Silverlight 使用JSON.NET 输出: ......
对比
主要类 | 命名空间 | 限制 | 内建linq支持 |
---|---|---|---|
datacontractjsonserializer | system.runtime.serialization.json | 通用 | 否 |
javascriptserializer | system.web.script.serialization | 只能在web环境使用 | 否 |
jsonarray、jsonobject、jsonvalue | system.json | 只能在silverlight中使用 | 是 |
jsonconvert、jarray、jobject、jvalue、jproperty | newtonsoft.json | 通用 | 是 |
准备数据
实体类:
[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);
使用silverlight
// using system.json var css = "{ \"#header\" : {background:\"red\"}, layout : [5,4,1],color:\"cyan\" }"; var style = jsonobject.parse(css) as jsonobject; ( from s in style where s.key == "color" select (string)s.value ).first().tostring(); // "cyan" // 更多操作 style["layout"][0] = 22; var hd = style["#header"]; style["body>div+p"] = hd; style.remove("#header"); var bd = new jsonobject(); bd["border"] = "1px solid cyan"; style["body>div+p"]["#meta"] = bd; style.tostring(); // {"layout":[22,4,1],"color":"cyan","body>div+p":{"background":"red","#meta":{"border":"1px solid cyan"}}}
使用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 } } ]
上一篇: 哥们,来几件呗!