﻿{"id":2113,"date":"2014-01-04T15:49:57","date_gmt":"2014-01-04T07:49:57","guid":{"rendered":"http:\/\/www.ntxz.net\/?p=2113"},"modified":"2014-01-04T15:49:57","modified_gmt":"2014-01-04T07:49:57","slug":"newtonsoft-json-%e5%8f%8d%e5%ba%8f%e5%88%97%e5%8c%96%e8%a7%a3%e6%9e%90%e5%a4%9a%e5%b1%82%e5%b5%8c%e5%a5%97json","status":"publish","type":"post","link":"http:\/\/www.ntxz.net\/?p=2113","title":{"rendered":"Newtonsoft.Json \u53cd\u5e8f\u5217\u5316\u89e3\u6790\u591a\u5c42\u5d4c\u5957JSON"},"content":{"rendered":"<p>\u5728.net 2.0\u4e2d\u63d0\u53d6\u8fd9\u6837\u7684json <\/p>\n<p>{&#8220;name&#8221;:&#8221;lily&#8221;,&#8221;age&#8221;:23,&#8221;addr&#8221;:{&#8220;city&#8221;:guangzhou,&#8221;province&#8221;:guangdong}}<\/p>\n<p>\u5f15\u7528\u547d\u540d\u7a7a\u95f4<\/p>\n<p>using Newtonsoft.Json;<br \/>\nusing Newtonsoft.Json.Linq;<\/p>\n<p>\u53ef\u4ee5\u628a\u4e0a\u9762\u7684JSON\u770b\u6210\u4e00\u4e2a\u5bf9\u8c61.\u4f60\u53ea\u8981\u5199\u5bf9\u5e94\u7684\u7c7b\u5373\u53ef<\/p>\n<p>public class UserInfo<\/p>\n<p>{<\/p>\n<p>public string name;<\/p>\n<p>public int age;<\/p>\n<p>public address addr;<\/p>\n<p>}<\/p>\n<p>public class address<\/p>\n<p>{<\/p>\n<p>public string city;<\/p>\n<p>public string province;<br \/>\n}<\/p>\n<p>\u7136\u540e\u5728\u89e3\u6790\u7684\u5730\u65b9\u8fd9\u6837\u5199:<\/p>\n<p>string jsonData=&#8221;{\\&#8221;name\\&#8221;:\\&#8221;lily\\&#8221;,\\&#8221;age\\&#8221;:23,\\&#8221;addr\\&#8221;:{\\&#8221;city\\&#8221;:guangzhou,\\&#8221;province\\&#8221;:guangdong}}&#8221;;<\/p>\n<p>UserInfo user=(UserInfo)JsonConvert.DeserializeObject(jsonData, typeof(UserInfo));<\/p>\n<p>\u5f97\u5230City\u7684\u503c\u53ea\u8981:user.addr.City;<\/p>\n<p>\u8fd9\u6837\u5b9e\u73b0\u4e5f\u884c<\/p>\n<p>JObject jsonObj = JObject.Parse(jsonData);<\/p>\n<p>string name=jsonObj [&#8220;name&#8221;].ToString();<\/p>\n<p>string age=jsonObj [&#8220;age&#8221;].ToString();<\/p>\n<p>string city=((JObject )jsonObj [&#8220;addr&#8221;])[&#8220;city&#8221;].ToString();<\/p>\n<p>string province=((JObject )jsonObj [&#8220;addr&#8221;])[&#8220;province&#8221;].ToString();<\/p>\n<p>\u5982\u4f55\u8fd9\u4e2ajson\u662f\u52a8\u6001\u7684\u5462\uff1f\u8b6c\u5982\u8ba9\u4f60\u8f93\u5165\u4e00\u4e2ajson\uff0c\u5982{&#8220;name&#8221;:&#8221;lily&#8221;,&#8221;age&#8221;:23,&#8221;addr&#8221;:{&#8220;city&#8221;:guangzhou,&#8221;province&#8221;:guangdong}}\uff1b \u7136\u540e\u8ba9\u4f60\u8f93\u5165\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5982city\uff0c\u7136\u540e\u7cfb\u7edf\u4f1a\u8f93\u51faguangzhou\u8fd9\u4e2a\u503c\uff0c\u90a3\u8fd9\u6837\u7684\u8bdd\uff0cjson\u5c31\u662f\u52a8\u6001\u751f\u6210\u7684\u4e86\uff0c\u6211\u60f3\u4e86\u89e3\u6709\u6ca1\u6709\u8bfb\u53d6\u8fd9\u6837\u7684json\u7684\u65b9\u6cd5\u3002\uff08\u6ce8\u610f\uff0cjson\u662f\u591a\u7ea7\u5d4c\u5957\u7684\u3002\uff09<\/p>\n<p>\u5c31\u7528\u904d\u5386<\/p>\n<p>public string GetJsonValue(JEnumerable<JToken> jToken,string key)<br \/>\n{<br \/>\nIEnumerator enumerator = jToken.GetEnumerator();<br \/>\nwhile (enumerator.MoveNext())<br \/>\n{<br \/>\nJToken jc = (JToken)enumerator.Current;<\/p>\n<p>if (jc is JObject||((JProperty)jc).Value is JObject)<br \/>\n{<br \/>\nreturn GetJsonValue(jc.Children(), key);<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nif (((JProperty)jc).Name == key)<br \/>\n{<\/p>\n<p>return ((JProperty)jc).Value.ToString();<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\nreturn null;<br \/>\n}<\/p>\n<p>\u5728\u8c03\u7528\u7684\u65f6\u5019:<\/p>\n<p>string jsonData = &#8220;{\\&#8221;name\\&#8221;:\\&#8221;lily\\&#8221;,\\&#8221;age\\&#8221;:23,\\&#8221;addr\\&#8221;:{\\&#8221;city\\&#8221;:\\&#8221;guangzhou\\&#8221;,\\&#8221;province\\&#8221;:\\&#8221;guangdong\\&#8221;}}&#8221;;<br \/>\nJObject jsonObj = JObject.Parse(jsonData);<br \/>\nResponse.Write(GetJsonValue(jsonObj.Children(), &#8220;province&#8221;));<\/p>\n<p>\u5982\u679c\u6709\u591a\u5c42\u5d4c\u5957\u7684\u6570\u7ec4<\/p>\n<p>string  jsonData = &#8220;{\\&#8221;addr\\&#8221;:[{\\&#8221;city\\&#8221;:\\&#8221;guangzhou\\&#8221;,\\&#8221;province\\&#8221;:\\&#8221;guangdong\\&#8221;},{\\&#8221;city\\&#8221;:\\&#8221;guiyang\\&#8221;,\\&#8221;province\\&#8221;:\\&#8221;guizhou\\&#8221;}]}&#8221;;<br \/>\nJObject  jsonObj = JObject.Parse(jsonData);<br \/>\nJArray  jar = JArray.Parse(jsonObj[&#8220;addr&#8221;].ToString());<br \/>\nJObject  j = JObject.Parse(jar[0].ToString());<br \/>\nResponse.Write(j[&#8220;city&#8221;]);<\/p>\n<p>JSON\u8f6cXML<\/p>\n<p>string xmlstr=((XmlDocument)JsonConvert.DeserializeXmlNode(jsonData)).InnerXml.ToString();<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728.net 2.0\u4e2d\u63d0\u53d6\u8fd9\u6837\u7684json {&#8220;name&#8221;:&#8221;lily&#038;#82 [&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":[578,575],"class_list":["post-2113","post","type-post","status-publish","format-standard","hentry","category-fromnetwork","tag-json","tag-newtonsoft-json"],"views":8946,"_links":{"self":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/2113","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=2113"}],"version-history":[{"count":1,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/2113\/revisions"}],"predecessor-version":[{"id":2114,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/2113\/revisions\/2114"}],"wp:attachment":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2113"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}