贵州人事考试信息网,未来蛊药医,音速启动怎么卸载
主要类 | 命名空间 | 限制 | 内建linq支持 |
---|---|---|---|
通用 | 否 | ||
只能在web环境使用 | 否 | ||
、、 | 只能在silverlight中使用 | 是 | |
、、、、 | 通用 | 是 |
实体类:
[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 };
帮助类:
// 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}}]
// 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);
// 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"}}}
// 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 } } ]
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Net Core Web Api项目与在NginX下发布的方法
asp.net core3.1 引用的元包dll版本兼容性问题解决方案
IdentityServer4实现.Net Core API接口权限认证(快速入门)
ASP.NET Core MVC通过IViewLocationExpander扩展视图搜索路径的实现
网友评论