浅谈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
原创粉丝点击