{"id":979,"date":"2017-03-21T18:19:27","date_gmt":"2017-03-21T10:19:27","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=979"},"modified":"2017-03-21T18:19:27","modified_gmt":"2017-03-21T10:19:27","slug":"android%e5%9f%ba%e7%a1%80%e6%8e%a7%e4%bb%b6%e4%b9%8b%e7%bd%91%e7%bb%9c%e8%a7%a3%e6%9e%90jsonobject%e4%b8%8ejsonarray%e7%9a%84%e5%9f%ba%e6%9c%ac%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/android\/2017\/03\/21\/181927979.html","title":{"rendered":"Android\u57fa\u7840\u63a7\u4ef6\u4e4b\u7f51\u7edc\u89e3\u6790JSONObject\u4e0eJSONArray\u7684\u57fa\u672c\u4f7f\u7528"},"content":{"rendered":"<p>1\u3001\u7f51\u7edcJSON<\/p>\n<pre line=\"1\" lang=\"json\">{\n    \"resultCode\": \"200\",\n    \"data\": [\n        {\n            \"brand\": \"Audi\",\n            \"type\": \"A8L W12\"\n        },\n        {\n            \"brand\": \"BMW\",\n            \"type\": \"760Li\"\n        },\n        {\n            \"brand\": \"Maybach\",\n            \"type\": \"S600\"\n        },\n        {\n            \"brand\": \"Rolls Royce\",\n            \"type\": \"Patntom\"\n        }\n    ]\n}<\/pre>\n<p>2\u3001\u7f51\u7edc\u8bf7\u6c42\u53c2\u8003\uff1a<a href=\"http:\/\/www.yusian.com\/blog\/android\/2017\/03\/21\/163309.html\" target=\"_blank\">Android\u5f00\u53d1\u57fa\u7840\u63a7\u4ef6HttpURLConnection\u7f51\u7edc\u8bf7\u6c42<\/a><br \/>\n3\u3001JSONObject\u76f8\u5f53\u4e8ehashMap\uff0c\u6709getString\u3001getJSONArray\u7b49\u65b9\u6cd5\u53d6\u503c\uff1b<br \/>\n4\u3001JSONArray\u76f8\u5f53\u4e8eArrayList\u96c6\u5408\uff0c\u5faa\u73af\u904d\u5386\u4ece\u4e2d\u53d6JSONObject\u5bf9\u8c61\uff1b<br \/>\n5\u3001\u5c06\u4e0a\u8ff0JSON\u8f6c\u6362\u6210Java\u5bf9\u8c61\uff0c\u65b0\u5efa\u4e00\u4e2aSAResponse\u7528\u6765\u63a5\u6536\u6574\u4e2aJSON\uff0c\u518d\u521b\u5efa\u4e00\u4e2aSAObject\u63a5\u6536\u5185\u90e8JSONObject\uff1b<br \/>\n\u5173\u952e\u4ee3\u7801<\/p>\n<pre line=\"1\" lang=\"java\">    public void responseJsonFromString(String str){\n        Log.i(\"SALog\", str);\n        SAResponse res = new SAResponse();\n        try {\n            \/\/ \u65b0\u5efa\u4e00\u4e2aJSONObject\u5bf9\u8c61\u63a5\u6536\u6574\u4e2aJSON\uff1b\n            JSONObject resJson = new JSONObject(str);\n            \/\/ getString\u53ef\u76f4\u63a5\u53d6\u51faJSONObject\u4e2d\u952e\u5bf9\u5e94\u7684\u503c\uff1b\n            res.setCode(resJson.getString(\"resultCode\"));\n            \/\/ getJSONArray\u53d6JSON\u5f53\u4e2d\u7684\u6570\u7ec4\n            JSONArray jsonArray = resJson.getJSONArray(\"data\");\n            ArrayList<SAObject> array = new ArrayList<>();\n            \/\/ \u904d\u5386JSONArray\u5e76\u5c06\u5176\u4e2d\u7684JSONObject\u5143\u7d20\u8f6c\u6362\u6210\u5bf9\u8c61\n            for (int i = 0; i < jsonArray.length(); i++){\n                JSONObject jsonObj = jsonArray.getJSONObject(i);\n                SAObject object = new SAObject();\n                object.setBrand(jsonObj.getString(\"brand\"));\n                object.setType(jsonObj.getString(\"type\"));\n                array.add(object);\n            }\n            res.setArray(array);\n        } catch (JSONException e) {\n            e.printStackTrace();\n        }\n        Log.i(\"SALog\", res.toString());\n    }<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u7f51\u7edcJSON { &#8220;resultCode&#8221;: &#8220;200&#8221;, &#8220;data&#8221;: [ { &#8220;brand&#8221;: &#8220;Audi&#8221;, &#8220;type&#8221;: &#8220;A8L W12&#8221; }, { &#8220;brand&#8221;: &#8220;BMW&#8221;, &#8220;type&#8221;: &#8220;760Li&#8221; }, { &#8220;brand&#8221;: &#8220;Maybach&#8221;, &#8220;type&#8221;: &#8220;S600&#8221; }, { &#8220;brand&#8221;: &#8220;Rolls Royce&#8221;, &#8220;type&#8221;: &#8220;Patntom&#8221; } ] } 2\u3001\u7f51\u7edc\u8bf7\u6c42\u53c2\u8003\uff1aAndroid\u5f00\u53d1\u57fa\u7840\u63a7\u4ef6HttpURLConnection\u7f51\u7edc\u8bf7\u6c42 3\u3001JSONObject\u76f8\u5f53\u4e8ehashMap\uff0c\u6709getString\u3001getJSONArray\u7b49\u65b9\u6cd5\u53d6\u503c\uff1b 4\u3001JSONArray\u76f8\u5f53\u4e8eArrayList\u96c6\u5408\uff0c\u5faa\u73af\u904d\u5386\u4ece\u4e2d\u53d6JSONObject\u5bf9\u8c61\uff1b 5\u3001\u5c06\u4e0a\u8ff0JSON\u8f6c\u6362\u6210Java\u5bf9\u8c61\uff0c\u65b0\u5efa\u4e00\u4e2aSAResponse\u7528\u6765\u63a5\u6536\u6574\u4e2aJSON\uff0c\u518d\u521b\u5efa\u4e00\u4e2aSAObject\u63a5\u6536\u5185\u90e8JSONObject\uff1b \u5173\u952e\u4ee3\u7801 public void responseJsonFromString(String str){ Log.i(&#8220;SALog&#8221;, str); SAResponse res = new SAResponse(); try { \/\/ \u65b0\u5efa\u4e00\u4e2aJSONObject\u5bf9\u8c61\u63a5\u6536\u6574\u4e2aJSON\uff1b JSONObject [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[20],"class_list":["post-979","post","type-post","status-publish","format-standard","hentry","category-android","tag-basics"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/979","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=979"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/979\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=979"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=979"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}