{"id":265,"date":"2012-11-29T09:59:33","date_gmt":"2012-11-29T09:59:33","guid":{"rendered":"e56954b4f6347e897f954495eab16a88"},"modified":"2012-11-29T09:59:33","modified_gmt":"2012-11-29T09:59:33","slug":"","status":"publish","type":"post","link":"https:\/\/www.xiaobo.li\/notes\/archives\/265","title":{"rendered":"Newtonsoft.Json\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217"},"content":{"rendered":"<p>\u8fd9\u91cc\u4e0b\u8f7d:http:\/\/www.newtonsoft.com\/products\/json\/<span style=\"font-weight:bold;\"><br \/>\n\u5b89\u88c5:<br \/>\n<\/span>&nbsp;&nbsp; 1<span style=\"font-weight:bold;\">.<\/span>\u89e3\u538b\u4e0b\u8f7d\u6587\u4ef6,\u5f97\u5230Newtonsoft.Json.dll<br \/>\n&nbsp;&nbsp; 2.\u5728\u9879\u76ee\u4e2d\u6dfb\u52a0\u5f15\u7528..<br \/>\n<span style=\"font-weight:bold;\">&nbsp;\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5728.net\u9879\u76ee\u4e2d:<\/span><br \/>\n&nbsp;<\/p>\n<div class=\"overflowpanel\">\n<div style=\"font-family:Courier New;background:white 0% 50%;color:black;font-size:9pt;moz-background-clip:-moz-initial;moz-background-origin:-moz-initial;moz-background-inline-policy:-moz-initial;\" class=\"code\">\n<pre style=\"margin:0px;\"><span style=\"color:teal;\">Product<\/span> product = <span style=\"color:blue;\">new<\/span> <span style=\"color:teal;\">Product<\/span>();<\/pre>\n<pre style=\"margin:0px;\">&nbsp;<\/pre>\n<pre style=\"margin:0px;\">product.Name = <span style=\"color:maroon;\">\"Apple\"<\/span>;<\/pre>\n<pre style=\"margin:0px;\">product.Expiry = <span style=\"color:blue;\">new<\/span> <span style=\"color:teal;\">DateTime<\/span>(2008, 12, 28);<\/pre>\n<pre style=\"margin:0px;\">product.Price = 3.99M;<\/pre>\n<pre style=\"margin:0px;\">product.Sizes = <span style=\"color:blue;\">new<\/span> <span style=\"color:blue;\">string<\/span>[] { <span style=\"color:maroon;\">\"Small\"<\/span>, <span style=\"color:maroon;\">\"Medium\"<\/span>, <span style=\"color:maroon;\">\"Large\"<\/span> };<\/pre>\n<pre style=\"margin:0px;\">&nbsp;<\/pre>\n<pre style=\"margin:0px;\"><span style=\"color:blue;\">string<\/span> output = <span style=\"color:teal;\">javascriptConvert<\/span>.SerializeObject(product);<\/pre>\n<pre style=\"margin:0px;\"><span style=\"color:green;\">\/\/{<\/span><\/pre>\n<pre style=\"margin:0px;\"><span style=\"color:green;\">\/\/&nbsp; \"Name\": \"Apple\",<\/span><\/pre>\n<pre style=\"margin:0px;\"><span style=\"color:green;\">\/\/&nbsp; \"Expiry\": new Date(1230422400000),<\/span><\/pre>\n<pre style=\"margin:0px;\"><span style=\"color:green;\">\/\/&nbsp; \"Price\": 3.99,<\/span><\/pre>\n<pre style=\"margin:0px;\"><span style=\"color:green;\">\/\/&nbsp; \"Sizes\": [<\/span><\/pre>\n<pre style=\"margin:0px;\"><span style=\"color:green;\">\/\/&nbsp;&nbsp;&nbsp; \"Small\",<\/span><\/pre>\n<pre style=\"margin:0px;\"><span style=\"color:green;\">\/\/&nbsp;&nbsp;&nbsp; \"Medium\",<\/span><\/pre>\n<pre style=\"margin:0px;\"><span style=\"color:green;\">\/\/&nbsp;&nbsp;&nbsp; \"Large\"<\/span><\/pre>\n<pre style=\"margin:0px;\"><span style=\"color:green;\">\/\/&nbsp; ]<\/span><\/pre>\n<pre style=\"margin:0px;\"><span style=\"color:green;\">\/\/}<\/span><\/pre>\n<pre style=\"margin:0px;\">&nbsp;<\/pre>\n<pre style=\"margin:0px;\"><span style=\"color:teal;\">Product<\/span> deserializedProduct = (<span style=\"color:teal;\">Product<\/span>)<span style=\"color:teal;\">javascriptConvert<\/span>.DeserializeObject(output, <span style=\"color:blue;\">typeof<\/span>(<span style=\"color:teal;\">Product<\/span>));<\/pre>\n<\/div>\n<\/div>\n<p>&nbsp; <br \/>\n\u8bfb\u53d6JSON<\/p>\n<div class=\"overflowpanel\">\n<div style=\"font-family:Courier New;background:white 0% 50%;color:black;font-size:9pt;moz-background-clip:-moz-initial;moz-background-origin:-moz-initial;moz-background-inline-policy:-moz-initial;\" class=\"code\">\n<pre style=\"margin:0px;\"><span style=\"color:blue;\">string<\/span> jsonText = <span style=\"color:maroon;\">\"['JSON!',1,true,{property:'value'}]\"<\/span>;<\/pre>\n<pre style=\"margin:0px;\">&nbsp;<\/pre>\n<pre style=\"margin:0px;\"><span style=\"color:teal;\">JsonReader<\/span> reader = <span style=\"color:blue;\">new<\/span> <span style=\"color:teal;\">JsonReader<\/span>(<span style=\"color:blue;\">new<\/span> <span style=\"color:teal;\">StringReader<\/span>(jsonText));<\/pre>\n<pre style=\"margin:0px;\">&nbsp;<\/pre>\n<pre style=\"margin:0px;\"><span style=\"color:teal;\">Console<\/span>.WriteLine(<span style=\"color:maroon;\">\"TokenType\\t\\tValueType\\t\\tValue\"<\/span>);<\/pre>\n<pre style=\"margin:0px;\">&nbsp;<\/pre>\n<pre style=\"margin:0px;\"><span style=\"color:blue;\">while<\/span> (reader.Read())<\/pre>\n<pre style=\"margin:0px;\">{<\/pre>\n<pre style=\"margin:0px;\">&nbsp;&nbsp;&nbsp; <span style=\"color:teal;\">Console<\/span>.WriteLine(reader.TokenType + <span style=\"color:maroon;\">\"\\t\\t\"<\/span> + WriteValue(reader.ValueType) + <span style=\"color:maroon;\">\"\\t\\t\"<\/span> + WriteValue(reader.Value))<\/pre>\n<pre style=\"margin:0px;\">}<\/pre>\n<\/div>\n<\/div>\n<p>\n\u7ed3\u679c\u663e\u793a:<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">\n<tbody>\n<tr>\n<th>TokenType<\/th>\n<th>ValueType<\/th>\n<th>Value<\/th>\n<\/tr>\n<tr>\n<td>StartArray <\/td>\n<td>null <\/td>\n<td>null <\/td>\n<\/tr>\n<tr>\n<td>String <\/td>\n<td>System.String <\/td>\n<td>JSON! <\/td>\n<\/tr>\n<tr>\n<td>Integer <\/td>\n<td>System.Int32 <\/td>\n<td>1 <\/td>\n<\/tr>\n<tr>\n<td>Boolean <\/td>\n<td>System.Boolean <\/td>\n<td>True <\/td>\n<\/tr>\n<tr>\n<td>StartObject <\/td>\n<td>null <\/td>\n<td>null <\/td>\n<\/tr>\n<tr>\n<td>PropertyName <\/td>\n<td>System.String <\/td>\n<td>property <\/td>\n<\/tr>\n<tr>\n<td>String <\/td>\n<td>System.String <\/td>\n<td>value <\/td>\n<\/tr>\n<tr>\n<td>EndObject <\/td>\n<td>null <\/td>\n<td>null <\/td>\n<\/tr>\n<tr>\n<td>EndArray <\/td>\n<td>null <\/td>\n<td>null<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>JSON\u5199\u5165<\/h4>\n<div class=\"overflowpanel\">\n<div style=\"font-family:Courier New;background:white 0% 50%;color:black;font-size:9pt;moz-background-clip:-moz-initial;moz-background-origin:-moz-initial;moz-background-inline-policy:-moz-initial;\" class=\"code\">\n<pre style=\"margin:0px;\"><span style=\"color:teal;\">StringWriter<\/span> sw = <span style=\"color:blue;\">new<\/span> <span style=\"color:teal;\">StringWriter<\/span>();<\/pre>\n<pre style=\"margin:0px;\"><span style=\"color:teal;\">JsonWriter<\/span> writer = <span style=\"color:blue;\">new<\/span> <span style=\"color:teal;\">JsonWriter<\/span>(sw);<\/pre>\n<pre style=\"margin:0px;\">&nbsp;<\/pre>\n<pre style=\"margin:0px;\">writer.WriteStartArray();<\/pre>\n<pre style=\"margin:0px;\">writer.WriteValue(<span style=\"color:maroon;\">\"JSON!\"<\/span>);<\/pre>\n<pre style=\"margin:0px;\">writer.WriteValue(1);<\/pre>\n<pre style=\"margin:0px;\">writer.WriteValue(<span style=\"color:blue;\">true<\/span>);<\/pre>\n<pre style=\"margin:0px;\">writer.WriteStartObject();<\/pre>\n<pre style=\"margin:0px;\">writer.WritePropertyName(<span style=\"color:maroon;\">\"property\"<\/span>);<\/pre>\n<pre style=\"margin:0px;\">writer.WriteValue(<span style=\"color:maroon;\">\"value\"<\/span>);<\/pre>\n<pre style=\"margin:0px;\">writer.WriteEndObject();<\/pre>\n<pre style=\"margin:0px;\">writer.WriteEndArray();<\/pre>\n<pre style=\"margin:0px;\">&nbsp;<\/pre>\n<pre style=\"margin:0px;\">writer.Flush();<\/pre>\n<pre style=\"margin:0px;\">&nbsp;<\/pre>\n<pre style=\"margin:0px;\"><span style=\"color:blue;\">string<\/span> jsonText = sw.GetStringBuilder().ToString();<\/pre>\n<pre style=\"margin:0px;\">&nbsp;<\/pre>\n<pre style=\"margin:0px;\"><span style=\"color:teal;\">Console<\/span>.WriteLine(jsonText);<\/pre>\n<pre style=\"margin:0px;\"><span style=\"color:green;\">\/\/ ['JSON!',1,true,{property:'value'}]<\/span><\/pre>\n<\/div>\n<\/div>\n<p>\n\u8fd9\u91cc\u4f1a\u6253\u5370\u51fa: <code>['JSON!',1,true,{property:'value'}]<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8bfb\u53d6JSON<\/p>\n<div class=\"overflowpanel\">\n<div style=\"font-family:Courier New;background:white 0% 50%;color:black;font-size:9pt;moz-background-clip:-moz-initial;moz-background-origin:-moz-initial;moz-background-inline-policy:-moz-initial;\" class=\"code\">\n<pre style=\"margin:0px;\"><span style=\"color:blue;\">string<\/span> jsonText = <span style=\"color:maroon;\">\"['JSON!',1,true,{property:'value'}]\"<\/span>;<\/pre>\n<pre style=\"margin:0px;\">&nbsp;<\/pre>\n<pre style=\"margin:0px;\"><span style=\"color:teal;\">JsonReader<\/span> reader = <span style=\"color:blue;\">new<\/span> <span style=\"color:teal;\">JsonReader<\/span>(<span style=\"color:blue;\">new<\/span> <span style=\"color:teal;\">StringReader<\/span>(jsonText));<\/pre>\n<pre style=\"margin:0px;\">&nbsp;<\/pre>\n<pre style=\"margin:0px;\"><span style=\"color:teal;\">Console<\/span>.WriteLine(<span style=\"color:maroon;\">\"TokenType\\t\\tValueType\\t\\tValue\"<\/span>);<\/pre>\n<\/div>\n<\/div>\n<p> <a href=\"https:\/\/www.xiaobo.li\/notes\/archives\/265\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[273],"tags":[88],"class_list":["post-265","post","type-post","status-publish","format-standard","hentry","category-article","tag-json"],"_links":{"self":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/265","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/comments?post=265"}],"version-history":[{"count":0,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/265\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/media?parent=265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/categories?post=265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/tags?post=265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}