浅谈json转c#对象
来源:互联网 发布:淘宝商品设置多个选项 编辑:程序博客网 时间:2024/06/05 09:49
<ol class="dp-c" style="margin-left: 55px; padding: 5px 0px; color: rgb(92, 92, 92); list-style-position: initial; list-style-image: initial; word-wrap: break-word; word-break: normal; border: none; background-color: rgb(247, 247, 247); font-family: 'Courier New', monospace; line-height: 28px; white-space: pre; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important;"><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">class</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> TestModel </span></span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> { </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">string</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> name { </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">get</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">; </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">set</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">; } </span></span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">int</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> age { </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">get</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">; </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">set</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">; } </span></span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> List<</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">string</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">> likes { </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">get</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">; </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">set</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">; } </span></span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> } </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> } </span></li></ol>
json字符串 和上面定义的类相对应
<pre name="code" class="csharp"><span style="font-family: 'Courier New', monospace; line-height: 18px; white-space: pre; background-color: rgb(247, 247, 247);"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: rgb(247, 247, 247); font-weight: bold; font-family: 'Courier New', monospace; line-height: 18px; white-space: pre;">string</span><span style="margin: 0px; padding: 0px; border: none; background-color: rgb(247, 247, 247); font-family: 'Courier New', monospace; line-height: 18px; white-space: pre;"> jsonStr = </span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: rgb(247, 247, 247); font-family: 'Courier New', monospace; line-height: 18px; white-space: pre;">"{\"name\":\"supperlitt\",\"age\":25,\"likes\":[\"C#\",\"asp.net\"]}"</span><span style="margin: 0px; padding: 0px; border: none; background-color: rgb(247, 247, 247); font-family: 'Courier New', monospace; line-height: 18px; white-space: pre;">; </span>
<ol class="dp-c" style="margin-left: 55px; padding: 5px 0px; color: rgb(92, 92, 92); list-style-position: initial; list-style-image: initial; word-wrap: break-word; word-break: normal; border: none; background-color: rgb(247, 247, 247); font-family: 'Courier New', monospace; line-height: 28px; white-space: pre; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important;"><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">JavaScriptSerializer js = <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> JavaScriptSerializer(); </span></span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> var model = js.Deserialize<TestModel>(jsonStr); </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> </span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> Console.WriteLine(model.name); </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> Console.WriteLine(model.age); </span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> Console.WriteLine(<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">string</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">.Join(</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">","</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, model.likes)); </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; color: inherit; list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> </span></li><li style="list-style: decimal outside none; word-wrap: break-word; word-break: normal; border: none; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-color: transparent; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-position: -498px -70px; background-repeat: no-repeat repeat;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> Console.ReadLine(); </span></li></ol>
<p style="margin-top: 10px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); background-color: rgb(248, 248, 248); text-indent: 28px; font-family: 宋体; font-size: 14px; line-height: 28px;">输出内容:</p><p style="margin-top: 10px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); background-color: rgb(248, 248, 248); text-indent: 28px; font-family: 宋体; font-size: 14px; line-height: 28px; text-align: center;"><img class="fit-image" alt="" src="http://s6.51cto.com/wyfs02/M01/6F/33/wKiom1WUpPCTNxSDAAAO58Ka0aU351.jpg" style="border: 0px;" /></p>
0 0
- 浅谈json转c#对象
- 浅谈json,对象,数组
- C#对象转json字符串和json字符串转对象
- c#中字符串转为json对象与json转对象
- C# HTTPWebRequest对象浅谈
- 浅谈C# String对象
- Json 转 指定 C#对象,给c#对象赋值
- c# json字符串转xml对象代码
- c# json字符串转xml对象代码
- c#字符串转json对象(一)
- 【C#】【JSON】JSON转C#的写法(含嵌套对象)
- JSON之Asp.net MVC C#对象转JSON,DataTable转JSON,List<T>转JSON,JSON转List<T>,JSON转C#对象
- JSON之Asp.net MVC C#对象转JSON,DataTable转JSON,List<T>转JSON,JSON转List<T>,JSON转C#对象
- C# json字符串转为对象
- C# 对象与json互转
- JSON与c#对象转换
- C# 对象与JSON互转
- json 对象的解析c#
- 学习篇--log4j.properties
- JavaScript之解释与执行
- codeforce 550c Divisibility by Eight (DFS)
- BlocksKit初见:一个支持将delegate转换成block的Cocoa库
- SpringMvc多视图整合配置教程(jsp、velocity、freemarker)
- 浅谈json转c#对象
- JavaScript基础(2):什么是“闭包”?
- win10+ubuntu14.04双系统有线网络问题
- 软件测试会大概问的一些问题整理续
- Linux 下 NFS服务的搭建
- javascript动态获取当前页面的url
- 分享:EditText默认不弹出软件键盘
- React Native Android Navigator的使用
- eclipse +maven实现多模块项目struts+spring+mybatis