﻿{"id":2108,"date":"2013-10-09T18:26:25","date_gmt":"2013-10-09T10:26:25","guid":{"rendered":"http:\/\/www.ntxz.net\/?p=2108"},"modified":"2013-10-09T18:26:25","modified_gmt":"2013-10-09T10:26:25","slug":"newtonsoft-json%e8%bf%9b%e8%a1%8c%e5%ba%8f%e5%88%97%e5%8c%96%e5%92%8c%e5%8f%8d%e5%ba%8f%e5%88%97%e5%8c%96-%e6%bc%94%e4%b9%a0","status":"publish","type":"post","link":"http:\/\/www.ntxz.net\/?p=2108","title":{"rendered":"NewTonSoft.Json\u8fdb\u884c\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316-\u6f14\u4e60"},"content":{"rendered":"<p>JSON\u5df2\u7ecf\u6d41\u884c\u5f88\u4e45\u4e86\uff0c\u5bf9\u4e8e\u5b83\u7684\u4e86\u89e3\u4e0d\u662f\u5f88\u6df1\uff0c\u6700\u8fd1\u516c\u53f8\u505a\u9879\u76ee\u9700\u8981\u5c3d\u53ef\u80fd\u5c11\u7684\u548c\u670d\u52a1\u5668\u8fdb\u884c\u901a\u8baf\uff01<\/p>\n<p>\u5fc3\u53ef\u80fd\u5c11\u7684\u548c\u670d\u52a1\u5668\u901a\u8baf\uff0c\u90a3\u610f\u601d\u662f\u8bf4\uff0c\u53ea\u8981\u83b7\u53d6\u6570\u636e\uff0c\u4e0d\u8981\u4efb\u4f55\u5176\u5b83\u201c\u6742\u4e03\u6742\u516b\u201d\u7684HTML\u4ee3\u7801\uff0c\u60f3\u60f3\uff0c\u53ea\u8981ajax\u5c31\u53ef\u4ee5\u4e86\u561b\uff0c\u4f46\u53c8\u60f3ajax\u53ea\u662f\u505a\u5230\u4e86\u5c40\u90e8\u5237\u65b0\uff0c\u6ca1\u6709\u505a\u5230\u53bb\u9664\u201c\u6742\u4e03\u6742\u516b\u201d\u7684HTML\u4ee3\u7801\uff0c\u83b7\u53d6\u56de\u6765\u8fd8\u662f\u6709DIV,SPAN,A,\u7b49\u7b49\u2026\u2026<\/p>\n<p>\u67e5\u4e86\u4e00\u4e0b\u8d44\u6599\uff0c\u53d1\u73b0JSON\u53ef\u4ee5\u5c3d\u53ef\u80fd\u7684\u6ee1\u8db3\u8fd9\u4e2a\u9700\u6c42\uff01<\/p>\n<p>\u4e00\u4e2a\u65b0\u4e8b\u7269\u7684\u8bde\u751f\uff0c\u4e0d\u662f\u5076\u7136\u7684\u6a2a\u7a7a\u51fa\u4e16\uff0c\u800c\u662f\u5e94\u8fd0\u800c\u751f\u7684\u3002\u6709\u9700\u6c42\uff0c\u5c31\u6709\u5e02\u573a\uff01<\/p>\n<p>\u5bf9\u4e8eJSON\u7684\u521d\u6b65\u4e86\u89e3\u5c31\u4e0d\u4ecb\u7ecd\u5566\uff0c\u81ea\u5df1GOOGLE BAIDU\u4e00\u4e0b\u3002\u6709\u5173 JSON \u7684\u66f4\u591a\u4ecb\u7ecd\uff0c\u8bbf\u95ee\u5b83\u7684\u5b98\u65b9\u7f51\u7ad9\uff1ahttp:\/\/www.json.org<\/p>\n<p>\u672c\u4eba\u4f7f\u7528C#\u5403\u996d\uff0c\u9996\u5148\u4e0b\u8f7d\u6700\u65b0\u7248\u7684 JSON.NET  \u6e90\u7801 \uff08http:\/\/json.codeplex.com\/\uff09\uff0c\u7136\u540e\u89e3\u538b\uff0cSRC \u76ee\u5f55\u662f\u5176\u6e90\u7801\uff0c\u6211\u4eec\u6253\u5f00 Newtonsoft.Json.Net20.sln,\u8fd9\u4e2a\u662f.NET 2.0 \u7684\u89e3\u51b3\u65b9\u6848\uff0c\u7136\u540e\u7f16\u8bd1\uff0c\u5f88\u987a\u5229\u7684\u5c31\u901a\u8fc7\u4e86\uff0c\u53cd\u6b63\u6211\u662f\u6ca1\u9047\u5565\u7279\u6b8a\u60c5\u51b5\u3002\u7f16\u8bd1\u5b8c\u6210\u540e\u5728 bin\\Release  \u76ee\u5f55\u4e2d\u4f60\u4f1a\u770b\u5230\u4e00\u4e2a\u540d\u4e3a DotNet20  \u7684\u76ee\u5f55\uff0c\u5f88\u663e\u7136\u8fd9\u4e2a JSON.net \u7684\u6e90\u7801\u505a\u7684\u76f8\u5f53\u4e0d\u9519\uff0c\u5df2\u7ecf\u4e3a\u6211\u4eec\u5199\u597d\u4e86\u5728.NET2.0 \u4e0b\u8fd0\u884c\u7684\u65b9\u6cd5\u3002\u5f15\u7528\u5230\u9879\u76ee\u5f53\u4e2d\u3002<\/p>\n<p>\u6dfb\u52a0\u4e0b\u9762\u4e24\u53e5\uff1a<\/p>\n<p><code><br \/>\nusing Newtonsoft.Json;<br \/>\nusing Newtonsoft.Json.Converters;<br \/>\n<\/code><\/p>\n<p>\u4ee3\u7801\uff1a<\/p>\n<p>==================================================<\/p>\n<pre class=\"brush: c#\">\r\nDataTable dt= new DataTable();\r\n            dt.Columns.Add(&quot;a&quot;,Type.GetType(&quot;System.String&quot;));\r\n            dt.Columns.Add(&quot;b&quot;, Type.GetType(&quot;System.String&quot;));\r\n            dt.Columns.Add(&quot;c&quot;, Type.GetType(&quot;System.String&quot;));\r\n\r\n            for (int i = 0; i &lt; 10; i++)\r\n            {\r\n                DataRow dr = dt.NewRow();\r\n                dr[&quot;a&quot;] = &quot;1&quot; + i.ToString();\r\n                dr[&quot;b&quot;] = &quot;2&quot; + i.ToString();\r\n                dr[&quot;c&quot;] = &quot;3&quot; + i.ToString();\r\n                dt.Rows.Add(dr);\r\n            }\r\n\r\n            string sj = JsonConvert.SerializeObject(dt,new DataTableConverter());\r\n\r\n            \/\/string dt1 = (string)JsonConvert.DeserializeObject(strJson);\r\n\r\n\r\n           Response.Write(sj);\r\n\r\n<\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<p>[{&#8220;a&#8221;:&#8221;10&#8243;,&#8221;b&#8221;:&#8221;20&#8243;,&#8221;c&#8221;:&#8221;30&#8243;},{&#8220;a&#8221;:&#8221;11&#8243;,&#8221;b&#8221;:&#8221;21&#8243;,&#8221;c&#8221;:&#8221;31&#8243;},{&#8220;a&#8221;:&#8221;12&#8243;,&#8221;b&#8221;:&#8221;22&#8243;,&#8221;c&#8221;:&#8221;32&#8243;},{&#8220;a&#8221;:&#8221;13&#8243;,&#8221;b&#8221;:&#8221;23&#8243;,&#8221;c&#8221;:&#8221;33&#8243;},{&#8220;a&#8221;:&#8221;14&#8243;,&#8221;b&#8221;:&#8221;24&#8243;,&#8221;c&#8221;:&#8221;34&#8243;},{&#8220;a&#8221;:&#8221;15&#8243;,&#8221;b&#8221;:&#8221;25&#8243;,&#8221;c&#8221;:&#8221;35&#8243;},{&#8220;a&#8221;:&#8221;16&#8243;,&#8221;b&#8221;:&#8221;26&#8243;,&#8221;c&#8221;:&#8221;36&#8243;},{&#8220;a&#8221;:&#8221;17&#8243;,&#8221;b&#8221;:&#8221;27&#8243;,&#8221;c&#8221;:&#8221;37&#8243;},{&#8220;a&#8221;:&#8221;18&#8243;,&#8221;b&#8221;:&#8221;28&#8243;,&#8221;c&#8221;:&#8221;38&#8243;},{&#8220;a&#8221;:&#8221;19&#8243;,&#8221;b&#8221;:&#8221;29&#8243;,&#8221;c&#8221;:&#8221;39&#8243;}]<\/p>\n<p>\u4ee5\u4e0a\u5b9e\u73b0\u4e86datatable\u5e8f\u5217\u5316\uff01\uff01<\/p>\n<p>\u518d\u901a\u8fc7\u4e0b\u9762\u8fd9\u4e09\u540d\uff0c\u53ef\u4ee5\u628aDataset\u4f20\u7ed9Datatabel\uff0c\u5c31\u53ef\u4ee5\u5b9e\u73b0\u6570\u636e\u5e93\u5230json\u7684\u5e8f\u5217\u5316<\/p>\n<p>DataSet ds = \u5f97\u5230\u6570\u636e\u96c6;<br \/>\nif (ds.Tables.Count > 0)<br \/>\nDataTable dt = ds.Tables[0];<\/p>\n<p>\u5176\u5b9e\u5f88\u7b80\u5355\u5427!!<\/p>\n<p>\u4e0b\u9762\u4ecb\u7ecdList\u548c Dectionary\u96c6\u5408\u7684\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\uff1a<\/p>\n<p><strong>List\u5e8f\u5217\u5316:<\/strong><\/p>\n<pre class=\"brush: c#\">\r\nList&lt;Customer&gt; _list = new List&lt;Customer&gt;();\r\n \r\n_list.Add(new Customer {  Unid=1, CustomerName=&quot;\u5b8b\u6c5f&quot;,Memo=&quot;\u5929\u9b41\u661f&quot;});\r\n_list.Add(new Customer { Unid = 1, CustomerName = &quot;\u5434\u7528&quot;, Memo = &quot;\u5929\u673a\u661f&quot; });\r\n_list.Add(new Customer { Unid = 1, CustomerName = &quot;\u77f3\u79c0&quot;, Memo = &quot;\u5929\u6167\u661f&quot; });\r\n \r\nstring strJson = JsonConvert.SerializeObject(_list, Formatting.Indented);\r\n \r\n\/*\r\n[{&quot;ID&quot;: 1,&quot;CustomerName&quot;: &quot;\u5b8b\u6c5f&quot;,&quot;Memo&quot;: &quot;\u5929\u9b41\u661f&quot;},\r\n  {&quot;ID&quot;: 1,&quot;CustomerName&quot;: &quot;\u5434\u7528&quot;,&quot;Memo&quot;: &quot;\u5929\u673a\u661f&quot;},\r\n  {&quot;ID&quot;: 1,&quot;CustomerName&quot;: &quot;\u77f3\u79c0&quot;,&quot;Memo&quot;: &quot;\u5929\u6167\u661f&quot;}]\r\n*\/\r\n<\/pre>\n<p><strong>2\uff09List\u53cd\u5e8f\u5217\u5316<\/strong><br \/>\n\u53cd\u5e8f\u5217\u5316\u4e00\u4e2a json \u5bf9\u8c61\u5230\u4e00\u4e2a.net \u96c6\u5408\uff0c\u53ea\u9700\u6307\u5b9a\u8981\u53cd\u5e8f\u5217\u5316\u7684\u96c6\u5408\u7c7b\u578b\u5373\u53ef\u3002<br \/>\njson.net \u652f\u6301\u5927\u91cf\u7684\u96c6\u5408\u7c7b\u578b\u3002<\/p>\n<p>string strJson = @&#8221;[{&#8220;&#8221;ID&#8221;&#8221;: 1,&#8221;&#8221;CustomerName&#8221;&#8221;: &#8220;&#8221;\u5b8b\u6c5f&#8221;&#8221;,&#8221;&#8221;Memo&#8221;&#8221;: &#8220;&#8221;\u5929\u9b41\u661f<br \/>\n&#8220;&#8221;},{&#8220;&#8221;ID&#8221;&#8221;: 1,&#8221;&#8221;CustomerName&#8221;&#8221;: &#8220;&#8221; \u5434\u7528 &#8220;&#8221;,&#8221;&#8221;Memo&#8221;&#8221;: &#8220;&#8221; \u5929\u673a\u661f &#8220;&#8221;},{&#8220;&#8221;ID&#8221;&#8221;:<br \/>\n1,&#8221;&#8221;CustomerName&#8221;&#8221;: &#8220;&#8221;\u77f3\u79c0&#8221;&#8221;,&#8221;&#8221;Memo&#8221;&#8221;: &#8220;&#8221;\u5929\u6167\u661f&#8221;&#8221;}]&#8221;;<\/p>\n<pre class=\"brush: c#\">\r\nList&lt;Customer&gt; _list = JsonConvert.DeserializeObject&lt;List&lt;Customer&gt;&gt;(strJson);\r\nforeach (Customer c in _list)\r\n{\r\n    Console.WriteLine(c.CustomerName);\r\n}\r\n \r\n\/*\r\n\u5b8b\u6c5f\r\n\u5434\u7528\r\n\u77f3\u79c0\r\n*\/\r\n \r\n<\/pre>\n<p><strong>3\uff09Dictionary\u53cd\u5e8f\u5217\u5316 <\/strong><br \/>\njson.net \u4e5f\u80fd\u628a json \u5bf9\u8c61\u53cd\u5e8f\u5217\u5316\u4e3a\u4e00\u4e2a\u6cdb\u578b\u5b57\u5178\u3002json \u5bf9\u8c61\u7684\u5c5e\u6027\u540d\u548c\u503c\u4f1a\u88ab\u6dfb\u52a0\u5230\u4e00\u4e2a\u5b57\u5178\u4e2d\u3002<\/p>\n<pre class=\"brush: c#\">\r\nstring strJson = @&quot;{&quot;&quot;ID&quot;&quot;: 1,&quot;&quot;CustomerName&quot;&quot;: &quot;&quot;\u5b8b\u6c5f&quot;&quot;,&quot;&quot;Memo&quot;&quot;: &quot;&quot;\u5929\u9b41\u661f&quot;&quot;}&quot;;\r\n \r\nDictionary&lt;string, string&gt; _dictionary = JsonConvert.DeserializeObject&lt;Dictionary&lt;string,\r\nstring&gt;&gt;(strJson); \r\n \r\nforeach (KeyValuePair&lt;string, string&gt; kp in _dictionary)\r\n{\r\n    Console.WriteLine(kp.Key + &quot;:&quot; + kp.Value);\r\n}\r\n<\/pre>\n<p>\u770b\u5230\u8fd9\u91cc\u662f\u4e0d\u662f\u6709\u4e9b\u201c\u61f5\u201d\uff0c\u8fd9\u4e0d\u5c31\u662f\u96c6\u5408\u7684\u201c\u5b57\u7b26\u4e32\u201d\u5316\u5417\uff1f\u6ca1\u4ec0\u4e48\u5927\u4e0d\u4e86\u7684\u3002\u975e\u4e5f\uff0c\u8fd9\u53ea\u662f\u670d\u52a1\u5668\u7aef\u7684\u54cd\u5e94\uff0c\u8981\u7ed3\u5408\u8d77\u5ba2\u6237\u7aef\u7684json2.js+jqery\u6267\u884c\u8d77\u6765\uff0c\u6548\u679c\uff0c\u4e00\u822c\u4eba\u6211\u4e0d\u544a\u8bc9\u4ed6\uff01<\/p>\n<p>=========================================================<\/p>\n<p>CS\u4ee3\u7801\uff1a<\/p>\n<pre class=\"brush: c#\">\r\n   DataTable dt= new DataTable();\r\n            dt.Columns.Add(&quot;a&quot;,Type.GetType(&quot;System.String&quot;));\r\n            dt.Columns.Add(&quot;b&quot;, Type.GetType(&quot;System.String&quot;));\r\n            dt.Columns.Add(&quot;c&quot;, Type.GetType(&quot;System.String&quot;));\r\n            for (int i = 0; i &lt; 10; i++)\r\n            {\r\n                DataRow dr = dt.NewRow();\r\n                dr[&quot;a&quot;] = &quot;1&quot; + i.ToString();\r\n                dr[&quot;b&quot;] = &quot;2&quot; + i.ToString();\r\n                dr[&quot;c&quot;] = &quot;3&quot; + i.ToString();\r\n                dt.Rows.Add(dr);\r\n            }\r\n            string sj = JsonConvert.SerializeObject(dt,new DataTableConverter());\r\n           Response.Write(sj);\r\n           Response.End();\r\n<\/pre>\n<p>======================================================<\/p>\n<p>\u770b\u4e0b\u9762\u5ba2\u6237\u7aef\u7684\u8c03\u7528\u4ee3\u7801\uff1a<\/p>\n<pre class=\"brush: html\">\r\n&lt;!DOCTYPE html PUBLIC &quot;-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN&quot; &quot;http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd&quot;&gt;\r\n&lt;html xmlns=&quot;http:\/\/www.w3.org\/1999\/xhtml&quot;&gt;\r\n&lt;head runat=&quot;server&quot;&gt;\r\n    &lt;title&gt;&lt;\/title&gt;\r\n    &lt;script type=&quot;text\/javascript&quot; src=&quot;Scripts\/jquery-1.4.1.min.js&quot;&gt;&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n    &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;\r\n    &lt;div style=&quot; margin:50px;&quot; id=&quot;f&quot;&gt;\r\n    &lt;\/div&gt;\r\n    &lt;\/form&gt;\r\n    &lt;script type=&quot;text\/javascript&quot;&gt;\r\n        $.getJSON(&quot;default.aspx&quot;, function (data) {\r\n            var ss = &quot;&quot;;\r\n            for (var i = 0; i &lt; getJsonLength(data); i++) {\r\n                ss += data[i].a + data[i].b + data[i].c + &quot;&lt;br&gt;&quot;;\r\n            }\r\n            $(&quot;#f&quot;).html(ss);\r\n        });\r\n        function getJsonLength(json) {\r\n            var len = 0;\r\n            if (Boolean(json)) {\r\n                for (i in json) len++;\r\n            }\r\n            return len;\r\n        }\r\n    &lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>\u7b80\u5355\u7684\u8bf4\u660e\u4e86\u4e00\u4e0b\u6574\u4e2ajson\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\uff0c\u5ba2\u6237\u7aef\u7684\u4f7f\u7528\uff0c\u6709\u4ec0\u4e48\u4e0d\u660e\u767d\u7684\u5c31\u767e\u5ea6+\u8c37\u6b4c\uff01<br \/>\n====================================<\/p>\n<p> \u8865\u5145\u8bf4\u660e\u4e00\u4e0b\uff0c\u5728\u5e8f\u5217\u5316\u65e5\u671f\u578b\u7684\u6570\u636e\u65f6\uff0c\u9700\u8981\u52a0\u4e0a\u4e00\u4e2a<\/p>\n<p>JavaScriptDateTimeConverter mTimeConverter = new JavaScriptDateTimeConverter();<br \/>\n\u6216\u8005<br \/>\nIsoDateTimeConverter mTimeConverter = new IsoDateTimeConverter(); <\/p>\n<p>\u7684\u683c\u5f0f\u8bf4\u660e\uff01IsoDateTimeConverter\u548cJavaScriptDateTimeConverter\u7c7b\u90fd\u662f\u6765\u81f3Newtonsoft.Json\uff0c\u6240\u4ee5\u5728\u4f7f\u7528\u65f6\uff0c\u8bb0\u5f97\u5f15\u7528\u8be5\u63d2\u4ef6\uff08using Newtonsoft.Json\u3001using Newtonsoft.Json.Converters\uff09<\/p>\n<p>\u6bd4\u5982\uff1a<\/p>\n<p>\u4e0a\u9762\u7684dr[&#8220;c&#8221;] = &#8220;3&#8221; + i.ToString();\u6539\u6210dr[&#8220;c&#8221;] = DateTime.Now;<\/p>\n<p>\u90a3\u4e48\u5e8f\u5217\u5316\u7684\u65f6\u5019\u5c31\u8981\u6539\u6210<br \/>\nstring sj = JsonConvert.SerializeObject(dt,new DataTableConverter(),new IsoDateTimeConverter());<\/p>\n<p>\u6216\u8005<br \/>\nstring sj = JsonConvert.SerializeObject(dt,new DataTableConverter(),JavaScriptDateTimeConverter());<\/p>\n<p>\u8fd4\u56de\u7684\u503c\u683c\u5f0f\u662f\u4e0d\u4e00\u6837\u7684\uff0c\u4e00\u4e2a\u662f\u65f6\u95f4\u6233\uff0c\u4e00\u4e2a\u662f\u5177\u4f53\u7684\u65f6\u5019\u5b57\u7b26\u4e32\u3002<\/p>\n<p> \u7ed3\u4e86\uff0c\u5199\u5f97\u6709\u70b9\u4e71\uff0c\u540e\u671f\u6709\u65f6\u95f4\u5c31\u6574\u7406\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JSON\u5df2\u7ecf\u6d41\u884c\u5f88\u4e45\u4e86\uff0c\u5bf9\u4e8e\u5b83\u7684\u4e86\u89e3\u4e0d\u662f\u5f88\u6df1\uff0c\u6700\u8fd1\u516c\u53f8\u505a\u9879\u76ee\u9700\u8981\u5c3d\u53ef\u80fd\u5c11\u7684\u548c\u670d\u52a1\u5668\u8fdb\u884c\u901a\u8baf\uff01 \u5fc3\u53ef\u80fd\u5c11\u7684\u548c\u670d\u52a1 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[575,577,576],"class_list":["post-2108","post","type-post","status-publish","format-standard","hentry","category-fromnetwork","tag-newtonsoft-json","tag-577","tag-576"],"views":4395,"_links":{"self":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/2108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2108"}],"version-history":[{"count":1,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/2108\/revisions"}],"predecessor-version":[{"id":2109,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/2108\/revisions\/2109"}],"wp:attachment":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2108"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}