{"id":942,"date":"2015-08-11T11:21:43","date_gmt":"2015-08-11T03:21:43","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=942"},"modified":"2021-04-12T10:54:08","modified_gmt":"2021-04-12T02:54:08","slug":"json%e6%95%b0%e6%8d%ae%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=942","title":{"rendered":"Json\u6570\u636e\u89e3\u6790"},"content":{"rendered":"<p>Json\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f,\u91c7\u7528\u5b8c\u5168\u72ec\u7acb\u4e8e\u8bed\u8a00\u7684\u6587\u672c\u683c\u5f0f.\u6613\u4e8e\u9605\u8bfb\u548c\u7f16\u5199\uff0c\u540c\u65f6\u4e5f\u6613\u4e8e\u673a\u5668\u89e3\u6790\u548c\u751f\u6210.\u652f\u6301\u591a\u79cd\u8bed\u8a00\u5305\u62ecC, C++, C#, Java, JavaScript, Perl, Python\u7b49\uff09.<br \/>\n<!--more--><\/p>\n<p><strong>JSON\u4e66\u5199\u683c\u5f0f<\/strong><\/p>\n<p>JSON\u7684\u89c4\u5219\u5f88\u7b80\u5355\uff1a\u5bf9\u8c61\u662f\u4e00\u4e2a\u65e0\u5e8f\u7684\u201c\u2018\u540d\u79f0\/\u503c\u2019\u5bf9\u201d\u96c6\u5408\u3002<br \/>\n\u4e00\u4e2a\u5bf9\u8c61\u4ee5\u201c{\u201d\uff08\u5de6\u62ec\u53f7\uff09\u5f00\u59cb\uff0c\u201c}\u201d\uff08\u53f3\u62ec\u53f7\uff09\u7ed3\u675f \u6bcf\u4e2a\u201c\u540d\u79f0\u201d\u540e\u8ddf\u4e00\u4e2a\u201c:\u201d\uff08\u5192\u53f7\uff09 \u201c\u2018\u540d\u79f0\/\u503c\u2019\u5bf9\u201d\u4e4b\u95f4\u4f7f\u7528\u201c,\u201d\uff08\u9017\u53f7\uff09\u5206\u9694\u3002<\/p>\n<p><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/08\/\u256c\u0432\u2568\u253c\u255c\u256a\u2550\u255d_20210412105246.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-3235\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/08\/\u256c\u0432\u2568\u253c\u255c\u256a\u2550\u255d_20210412105246.png\" alt=\"\u256c\u0432\u2568\u253c\u255c\u256a\u2550\u255d_20210412105246\" width=\"1101\" height=\"524\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>\u89c4\u5219\u5982\u4e0b:<br \/>\n1\uff09 \u6620\u5c04\u7528\u5192\u53f7\uff08\u201c\uff1a\u201d\uff09\u8868\u793a\u3002\u540d\u79f0:\u503c<br \/>\n2\uff09 \u5e76\u5217\u7684\u6570\u636e\u4e4b\u95f4\u7528\u9017\u53f7\uff08\u201c\uff0c\u201d\uff09\u5206\u9694\u3002\u540d\u79f01:\u503c1,\u540d\u79f02:\u503c2<br \/>\n3\uff09 \u6620\u5c04\u7684\u96c6\u5408\uff08\u5bf9\u8c61\uff09\u7528\u5927\u62ec\u53f7\uff08\u201c{}\u201d\uff09\u8868\u793a\u3002{\u540d\u79f01:\u503c,\u540d\u79f02:\u503c2}<br \/>\n4\uff09 \u5e76\u5217\u6570\u636e\u7684\u96c6\u5408\uff08\u6570\u7ec4\uff09\u7528\u65b9\u62ec\u53f7(\u201c[]\u201d)\u8868\u793a\u3002<br \/>\n[<br \/>\n{\u540d\u79f01:\u503c,\u540d\u79f02:\u503c2},<br \/>\n{\u540d\u79f01:\u503c,\u540d\u79f02:\u503c2}<br \/>\n]<br \/>\n5\uff09\u5143\u7d20\u503c\u53ef\u5177\u6709\u7684\u7c7b\u578b\uff1astring, number, object, array, true, false, null<\/p>\n<p><strong>\u4f8b\u5b50<\/strong><\/p>\n<p>[java]\u4f8b1\u3001{ &quot;name&quot;: &quot;Obama&quot;}<\/p>\n<p>\u4f8b2\u3001{ &quot;name&quot;: &quot;Romney&quot;,&quot;age&quot;: 56}<\/p>\n<p>\u4f8b3\u3001{ &quot;city&quot;:{&quot;name&quot;: &quot;bj&quot;},&quot;weatherinfo&quot;:{&quot;weather&quot;: &quot;sunny&quot;}}<\/p>\n<p>\u4f8b4\u3001{<br \/>\n&quot;city&quot;:{&quot;name&quot;: &quot;\u5317\u4eac&quot;,\u201ccity_id\u201d:&quot;101010100&quot;},<br \/>\n&quot;weatherinfo&quot;:{&quot;weather&quot;: &quot;sunny&quot;,&quot;temp&quot;:&quot;29\u5ea6&quot;}<br \/>\n}<br \/>\n\u4f8b5\u3001<br \/>\n[<br \/>\n  { &quot;name&quot;: &quot;\u5f20\u4e09&quot;, &quot;age&quot;:22, &quot;email&quot;: &quot;zhangsan@qq.com&quot; },<br \/>\n  { &quot;name&quot;: &quot;\u674e\u56db&quot;, &quot;age&quot;:23, &quot;email&quot;: &quot;lisi@qq.com&quot;},<br \/>\n  { &quot;name&quot;: &quot;\u738b\u4e94&quot;, &quot;age&quot;:24, &quot;email&quot;: &quot;wangwu@qq.com&quot; }<br \/>\n]<\/p>\n<p>\u4f8b6\u3001<br \/>\n{ &quot;student&quot;:<br \/>\n  [<br \/>\n    { &quot;name&quot;: &quot;\u5f20\u4e09&quot;, &quot;age&quot;:22, &quot;email&quot;: &quot;zhangsan@qq.com&quot; },<br \/>\n    { &quot;name&quot;: &quot;\u674e\u56db&quot;, &quot;age&quot;:23, &quot;email&quot;: &quot;lisi@qq.com&quot;},<br \/>\n    { &quot;name&quot;: &quot;\u738b\u4e94&quot;, &quot;age&quot;:24, &quot;email&quot;: &quot;wangwu@qq.com&quot; }<br \/>\n  ]<br \/>\n}<br \/>\n[\/java]<\/p>\n<p><strong>\u5728Android\u4e2d\u4f7f\u7528json<\/strong><\/p>\n<p>JSONObject\u89e3\u6790\u4e3e\u4f8b<br \/>\n\u5728\u670d\u52a1\u5668\u4e0a\u6709\u4e00\u4e2ahtml\u6587\u4ef6http:\/\/www.weather.com.cn\/data\/sk\/101010100.html<br \/>\nhtml\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<p>[java]<br \/>\n{&quot;weatherinfo&quot;:<br \/>\n   {&quot;city&quot;:&quot;\u5317\u4eac&quot;,&quot;cityid&quot;:&quot;101010100&quot;,&quot;temp&quot;:&quot;21&quot;,&quot;WD&quot;:&quot;\u4e1c\u5357\u98ce&quot;<br \/>\n    ,&quot;WS&quot;:&quot;2  \u7ea7&quot;,&quot;SD&quot;:&quot;78%&quot;,&quot;WSE&quot;:&quot;2&quot;,&quot;time&quot;:&quot;21:10&quot;,&quot;isRadar&quot;:&quot;1&quot;<br \/>\n    ,&quot;Radar&quot;:&quot;JC_RADAR_AZ9010_JB&quot;}<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>\u89e3\u6790\u6b65\u9aa4<br \/>\n1\u3001\u8bfb\u53d6html\u6587\u4ef6\u6e90\u4ee3\u7801\uff0c\u83b7\u53d6\u4e00\u4e2ajson\u5b57\u7b26\u4e32<\/p>\n<p>[java]<br \/>\nInputStream in = conn.getInputStream();<br \/>\nString jsonStr = DataUtil.Stream2String(in);\/\/\u5c06\u6d41\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u7684\u5de5\u5177\u7c7b[\/java]<\/p>\n<p>2\u3001\u901a\u8fc7\u6784\u9020\u51fd\u6570\u5c06json\u5b57\u7b26\u4e32\u8f6c\u6362\u6210json\u5bf9\u8c61<\/p>\n<p>[java]JSONObject  jsonObject=new JSONObject(jsonStr)\uff1b[\/java]<\/p>\n<p>3\u3001\u4ecejson\u5bf9\u8c61\u4e2d\u83b7\u53d6\u4f60\u6240\u9700\u8981\u7684\u952e\u6240\u5bf9\u5e94\u7684\u503c<\/p>\n<p>[java]<br \/>\nJSONObject  json=jsonObject.getJSONObject(&quot;weatherinfo&quot;);<br \/>\nString city=json.getString(&quot;city&quot;);<br \/>\nString temp=json.getString(&quot;temp&quot;)[\/java]<\/p>\n<p>JSONArray\u89e3\u6790\u4e3e\u4f8b<br \/>\n\u5728\u670d\u52a1\u5668\u4e0a\u6709\u4e00\u4e2ajs\u6587\u4ef6http:\/\/192.168.1.101\/Web\/news.js\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<p>[java][<br \/>\n {<br \/>\n   title : &quot;\u56fd\u5bb6\u53d1\u6539\u59d4\uff1a\u53f0\u6e7e\u964d\u6cb9\u4ef7\u548c\u5927\u9646\u6ca1\u53ef\u6bd4\u6027&quot;,<br \/>\n   description : &quot;\u56fd\u5bb6\u53d1\u6539\u59d4\u526f\u4e3b\u4efb\u6731\u4e4b\u946b&quot;,<br \/>\n   image : &quot;http:\/\/192.168.1.101\/Web\/img\/a.jpg&quot;,<br \/>\n   comment : 163<br \/>\n },<br \/>\n {<br \/>\n  title : &quot;\u56fd\u5bb6\u53d1\u6539\u59d4\uff1a\u53f0\u6e7e\u964d\u6cb9\u4ef7\u548c\u5927\u9646\u6ca1\u53ef\u6bd4\u6027&quot;,<br \/>\n  description : &quot;\u56fd\u5bb6\u53d1\u6539\u59d4\u526f\u4e3b\u4efb\u6731\u4e4b\u946b&quot;,<br \/>\n  image : &quot;http:\/\/192.168.1.101\/Web\/img\/b.jpg&quot;,<br \/>\n  comment : 0<br \/>\n },<br \/>\n {<br \/>\n   title : &quot;\u56fd\u5bb6\u53d1\u6539\u59d4\uff1a\u53f0\u6e7e\u964d\u6cb9\u4ef7\u548c\u5927\u9646\u6ca1\u53ef\u6bd4\u6027&quot;,<br \/>\n   description : &quot;\u56fd\u5bb6\u53d1\u6539\u59d4\u526f\u4e3b\u4efb\u6731\u4e4b\u946b&quot;,<br \/>\n   image : &quot;http:\/\/192.168.1.101\/Web\/img\/c.jpg&quot;,<br \/>\n   comment : 0<br \/>\n }<br \/>\n];[\/java]<\/p>\n<p>\u89e3\u6790\u6b65\u9aa4<br \/>\n1\u3001\u8bfb\u53d6\u6587\u4ef6\u6e90\u4ee3\u7801\uff0c\u83b7\u53d6\u4e00\u4e2ajson\u5b57\u7b26\u4e32<\/p>\n<p>[java]String jsonStr = DataUtil.Stream2String(in);[\/java]<\/p>\n<p>2\u3001\u901a\u8fc7\u6784\u9020\u51fd\u6570\u5c06json\u5b57\u7b26\u4e32\u8f6c\u6362\u6210json\u6570\u7ec4<\/p>\n<p>[java]JSONArray array = new JSONArray(jsonStr);[\/java]<\/p>\n<p>3\u3001\u904d\u5386\u6570\u7ec4\uff0c\u83b7\u53d6\u6570\u7ec4\u4e2d\u6bcf\u4e00\u4e2ajson\u5bf9\u8c61\uff0c\u540c\u65f6\u53ef\u4ee5\u83b7\u53d6json\u5bf9\u8c61\u4e2d\u952e\u5bf9\u5e94\u7684\u503c<\/p>\n<p>[java]<br \/>\nfor (int i = 0; i &lt; array.length(); i++) {<br \/>\n   JSONObject obj = array.getJSONObject(i);<br \/>\n   String title=obj.getString(&quot;title&quot;);<br \/>\n   String description=obj.getString(&quot;description&quot;);<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>\u6ce8\u610f\uff1a 1\u3001json\u6570\u7ec4\u5e76\u975e\u5168\u662f\u7531json\u5bf9\u8c61\u7ec4\u6210\u7684\u6570\u7ec4<br \/>\n2\u3001json\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u6570\u636e\u7c7b\u578b\u53ef\u4ee5\u4e0d\u76f8\u540c<\/p>\n<p><strong>\u5982\u4f55\u5c06\u4e00\u4e2ajson\u5bf9\u8c61\u8f6c\u6362\u6210json\u5b57\u7b26\u4e32\uff1f<\/strong><\/p>\n<p>[java]<br \/>\n        Logs.d(&quot;=========\u751f\u6210json\u6570\u7ec4\u5b57\u7b26\u4e32========================&quot;);<br \/>\n\tJSONObject j1 = new JSONObject();<br \/>\n\tj1.put(&quot;userName&quot;, &quot;\u5f20\u4e09&quot;);<br \/>\n\tj1.put(&quot;passWord&quot;, &quot;123456&quot;);<\/p>\n<p>\tJSONObject j2 = new JSONObject();<br \/>\n\tj2.put(&quot;userName&quot;, &quot;\u674e\u56db&quot;);<br \/>\n\tj2.put(&quot;passWord&quot;, &quot;abcd&quot;);<\/p>\n<p>\tJSONArray array = new JSONArray();<br \/>\n\tarray.put(0, j1);<br \/>\n\tarray.put(1, j2);<br \/>\n\tLogs.v(array.toString());<\/p>\n<p>\tLogs.d(&quot;=========\u89e3\u6790json\u6570\u7ec4========================&quot;);<br \/>\n\tfor(int i = 0; i&lt;array.length(); i++){<br \/>\n\t\tJSONObject jsonObject = array.getJSONObject(i);<br \/>\n\t\tString userName = jsonObject.getString(&quot;userName&quot;);<br \/>\n\t        String passWord = jsonObject.getString(&quot;passWord&quot;);<br \/>\n\t\tLogs.v(&quot;userName :&quot;+userName+&quot;, passWord :&quot;+passWord);<\/p>\n<p>\t}<br \/>\n[\/java]<\/p>\n<p>}<\/p>\n<p><strong>fastjson\u751f\u6210\u548c\u89e3\u6790json\u6570\u636e<\/strong><\/p>\n<p>(\u4e3e\u4f8b\uff1a4\u79cd\u5e38\u7528\u7c7b\u578b:JavaBean,List,List,List&lt;Map&lt;String,Object&gt;)<\/p>\n<p>fastjson\u7b80\u4ecb\uff1a<br \/>\nFastjson\u662f\u4e00\u4e2aJava\u8bed\u8a00\u7f16\u5199\u7684\u9ad8\u6027\u80fd\u529f\u80fd\u5b8c\u5584\u7684JSON\u5e93\u3002fastjson\u91c7\u7528\u72ec\u521b\u7684\u7b97\u6cd5\uff0c\u5c06parse\u7684\u901f\u5ea6\u63d0\u5347\u5230\u6781\u81f4\uff0c\u8d85\u8fc7\u6240\u6709json\u5e93\uff0c\u5305\u62ec\u66fe\u7ecf\u53f7\u79f0\u6700\u5feb\u7684jackson\u3002\u5e76\u4e14\u8fd8\u8d85\u8d8a\u4e86google\u7684\u4e8c\u8fdb\u5236\u534f\u8baeprotocol buf\u3002Fastjson\u5b8c\u5168\u652f\u6301http:\/\/json.org\u7684\u6807\u51c6\uff0c\u4e5f\u662f\u5b98\u65b9\u7f51\u7ad9\u6536\u5f55\u7684\u53c2\u8003\u5b9e\u73b0\u4e4b\u4e00\u3002\u652f\u6301\u5404\u79cdJDK\u7c7b\u578b\u3002\u5305\u62ec\u57fa\u672c\u7c7b\u578b\u3001JavaBean\u3001Collection\u3001Map\u3001Enum\u3001\u6cdb\u578b\u7b49\u3002\u652f\u6301JDK 5\u3001JDK 6\u3001Android\u3001\u963f\u91cc\u4e91\u624b\u673a\u7b49\u73af\u5883\u3002<\/p>\n<p>\u4e00. fastjson\u751f\u6210json\u5b57\u7b26\u4e32(JavaBean,List,List,List&lt;Map&lt;String,Object&gt;)<\/p>\n<p>[java]\u00a0\u00a0 String jsonStrng = JSON.toJSONString(object);[\/java]<\/p>\n<p>\u4e8c. fastjson \u89e3\u6790json\u5b57\u7b26\u4e32\u4e3a\u56db\u79cd\u7c7b\u578b<\/p>\n<p>[java]\u00a0\u00a0<br \/>\n     1. JavaBean<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Person person = JSON.parseObject(jsonString, Person.class);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0 2. List&lt;JavaBean&gt;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 List&lt;Person&gt; listPerson =JSON.parseArray(jsonString, Person.class);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0 3. List&lt;String&gt;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 List&lt;String&gt; listString = JSON.parseArray(jsonString, String.class);<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0 4. List&lt;Map&lt;String,Object&gt;&gt;[\/java]<\/p>\n<p>&nbsp;<\/p>\n<p>\u53c2\u8003\uff1a<br \/>\n<a href=\"http:\/\/www.json.org.cn\/\"> JSON\u5b98\u7f51<\/a><br \/>\n<a href=\"http:\/\/www.codeproject.com\/Articles\/159450\/fastJSON\">FastJson<\/a><\/p>\n<p>&nbsp;<\/p>\n<h2>Object References<\/h2>\n<p>Consider the following JSON.<\/p>\n<pre>{\n  <span class=\"highlight\">'authors': [\n    {\n      'id': 1,\n      'name': 'Joshua Bloch'\n    },\n    {\n      'id': 2,\n      'name': 'Neal Gafter'\n    }\n  ]<\/span>,\n  'books': [\n    {\n      'title': 'Java Puzzlers: Traps, Pitfalls, and Corner Cases',\n      'isbn': '032133678X',\n      <span class=\"highlight\">'authors':[1, 2]<\/span>\n    },\n    {\n      'title': 'Effective Java (2nd Edition)',\n      'isbn': '0321356683', \n      <span class=\"highlight\">'authors':[1]<\/span>\n    }\n  ]\n}\n<\/pre>\n<p>The JSON object shown above comprise two authors and two books. The books have a reference to the authors through their ids as in this example the books\u2019 field <code>authors<\/code> only contains the authors\u2019 ids. This is quite a common scenario as this approach will reduce the size of the JSON object as duplicate objects are referred to by their id. The following image captures the new JSON object hierarchy.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Json\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f,\u91c7\u7528\u5b8c\u5168\u72ec\u7acb\u4e8e\u8bed\u8a00\u7684\u6587\u672c\u683c\u5f0f.\u6613\u4e8e\u9605\u8bfb\u548c\u7f16\u5199\uff0c\u540c\u65f6\u4e5f\u6613\u4e8e\u673a\u5668\u89e3\u6790\u548c\u751f\u6210.\u652f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/942"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=942"}],"version-history":[{"count":11,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/942\/revisions"}],"predecessor-version":[{"id":3237,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/942\/revisions\/3237"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=942"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}