{"id":7065,"date":"2025-06-01T13:20:21","date_gmt":"2025-06-05T05:57:20","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7065"},"modified":"2025-06-05T13:20:21","modified_gmt":"2025-06-05T05:57:20","slug":"https-badgameshow-com-steven-207","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-207\/","title":{"rendered":"Swift JSON \u8cc7\u6599\u89e3\u6790\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, JSON, \u8cc7\u6599\u89e3\u6790, Swift 2025, JSON \u89e3\u6790\u6700\u4f73\u5be6\u8e10\"><\/p>\n<h1>Swift JSON \u8cc7\u6599\u89e3\u6790\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>Swift \u662f\u4e00\u7a2e\u7531 Apple \u63a8\u51fa\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u80fd\u5920\u8b93\u958b\u767c\u8005\u5feb\u901f\u958b\u767c iOS\u3001macOS\u3001watchOS \u548c tvOS \u7684\u61c9\u7528\u7a0b\u5f0f\u3002\u5728\u958b\u767c\u61c9\u7528\u7a0b\u5f0f\u6642\uff0c\u6211\u5011\u7d93\u5e38\u6703\u9047\u5230\u8cc7\u6599\u89e3\u6790\u7684\u554f\u984c\uff0c\u800c JSON \u662f\u4e00\u7a2e\u5e38\u898b\u7684\u8cc7\u6599\u683c\u5f0f\u3002\u5728 Swift \u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <code>JSONSerialization<\/code> \u6216\u66f4\u73fe\u4ee3\u7684 <code>Codable<\/code> \u5354\u8b70\u4f86\u89e3\u6790 JSON \u8cc7\u6599\u3002<\/p>\n<h3>\u5982\u4f55\u4f7f\u7528 <code>JSONSerialization<\/code> \u4f86\u89e3\u6790 JSON \u8cc7\u6599<\/h3>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5c07 JSON \u8cc7\u6599\u8f49\u63db\u6210 <code>Data<\/code> \u7269\u4ef6\uff0c\u7136\u5f8c\u518d\u4f7f\u7528 <code>JSONSerialization<\/code> \u4f86\u89e3\u6790\u8cc7\u6599\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let jsonString = \"{\\\"name\\\": \\\"John Doe\\\", \\\"age\\\": 30}\"\nlet jsonData = Data(jsonString.utf8)\nlet jsonObject = try? JSONSerialization.jsonObject(with: jsonData, options: [])<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c<code>JSONSerialization<\/code> \u6703\u5c07\u8cc7\u6599\u8f49\u63db\u6210\u4e00\u500b <code>Any<\/code> \u7269\u4ef6\uff0c\u5b83\u53ef\u4ee5\u662f\u4e00\u500b <code>Array<\/code> \u6216 <code>Dictionary<\/code>\u3002\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <code>if let<\/code> \u6216 <code>guard let<\/code> \u4f86\u78ba\u8a8d\u8cc7\u6599\u7684\u578b\u614b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">if let jsonDict = jsonObject as? [String: Any] {\n    \/\/ \u8655\u7406 Dictionary\n    print(\"Name: \\(jsonDict[\"name\"] ?? \"\")\")\n    print(\"Age: \\(jsonDict[\"age\"] ?? \"\")\")\n} else if let jsonArray = jsonObject as? [Any] {\n    \/\/ \u8655\u7406 Array\n    print(\"Array: \\(jsonArray)\")\n}<\/code><\/pre>\n<h3>\u4f7f\u7528 <code>Codable<\/code> \u5354\u8b70\u4f86\u89e3\u6790 JSON<\/h3>\n<p>\u5f9e Swift 4 \u958b\u59cb\uff0c<code>Codable<\/code> \u5354\u8b70\u63d0\u4f9b\u4e86\u4e00\u500b\u66f4\u7c21\u6f54\u7684\u65b9\u5f0f\u4f86\u89e3\u6790 JSON\u3002\u900f\u904e\u5b9a\u7fa9\u4e00\u500b\u7d50\u69cb\u4f86\u5c0d\u61c9 JSON \u7684\u7d50\u69cb\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u5730\u9032\u884c\u8cc7\u6599\u89e3\u6790\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">struct User: Codable {\n    let name: String\n    let age: Int\n}\n\nlet decoder = JSONDecoder()\nif let jsonData = jsonString.data(using: .utf8) {\n    do {\n        let user = try decoder.decode(User.self, from: jsonData)\n        print(\"Name: \\(user.name), Age: \\(user.age)\")\n    } catch {\n        print(\"Error decoding JSON: \\(error)\")\n    }\n}<\/code><\/pre>\n<h3>\u5982\u4f55\u5c07\u8cc7\u6599\u8f49\u63db\u6210 JSON \u683c\u5f0f<\/h3>\n<p>\u7576\u6211\u5011\u9700\u8981\u5c07\u8cc7\u6599\u8f49\u63db\u6210 JSON \u683c\u5f0f\u6642\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>JSONSerialization<\/code> \u7684 <code>data(withJSONObject:options:)<\/code> \u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let jsonObject: [String: Any] = [\n    \"name\": \"John Doe\",\n    \"age\": 30\n]\n\nlet jsonData = try? JSONSerialization.data(withJSONObject: jsonObject, options: [])\nif let jsonString = String(data: jsonData!, encoding: .utf8) {\n    print(jsonString) \/\/ {\"name\":\"John Doe\",\"age\":30}\n}<\/code><\/pre>\n<h3>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h3>\n<p>\u5728\u9032\u884c JSON \u89e3\u6790\u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\u683c\u5f0f\u932f\u8aa4\u548c\u8cc7\u6599\u578b\u614b\u4e0d\u5339\u914d\u3002\u78ba\u4fdd JSON \u683c\u5f0f\u6b63\u78ba\uff0c\u4e14\u5728\u89e3\u6790\u6642\u4f7f\u7528\u6b63\u78ba\u7684\u8cc7\u6599\u578b\u614b\u4f86\u907f\u514d\u5d29\u6f70\u3002\u6b64\u5916\uff0c\u7576\u8655\u7406\u5927\u578b JSON \u8cc7\u6599\u6642\uff0c\u8003\u616e\u4f7f\u7528 <code>Codable<\/code> \u4f86\u7c21\u5316\u89e3\u6790\u6d41\u7a0b\uff0c\u9019\u4e0d\u50c5\u63d0\u5347\u53ef\u8b80\u6027\uff0c\u9084\u80fd\u6e1b\u5c11\u932f\u8aa4\u767c\u751f\u7684\u6a5f\u6703\u3002<\/p>\n<h3>\u5ef6\u4f38\u61c9\u7528<\/h3>\n<p>\u9664\u4e86\u7c21\u55ae\u7684 JSON \u89e3\u6790\uff0c\u4f60\u53ef\u4ee5\u5c07 JSON \u8207\u7db2\u8def\u8acb\u6c42\u7d50\u5408\uff0c\u5f9e API \u7372\u53d6\u8cc7\u6599\uff0c\u4e26\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u9032\u884c\u89e3\u6790\u3002\u9019\u5728\u958b\u767c\u73fe\u4ee3\u7684 iOS \u61c9\u7528\u7a0b\u5f0f\u6642\u76f8\u7576\u6709\u7528\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-JSON-\u8cc7\u6599\u89e3\u6790-\ud83d\udcbb_1675401700.305016.webp\" alt=\"Swift JSON \u8cc7\u6599\u89e3\u6790 \ud83d\udcbb\" title=\"Swift JSON \u8cc7\u6599\u89e3\u6790\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 Swift \u8655\u7406 JSON \u8cc7\u6599\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. Swift \u4e2d JSON \u89e3\u6790\u5e38\u898b\u7684\u932f\u8aa4\u662f\u4ec0\u9ebc\uff1f<\/h3>\n<p>\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec JSON \u683c\u5f0f\u4e0d\u6b63\u78ba\u3001\u8cc7\u6599\u578b\u614b\u4e0d\u5339\u914d\u53ca\u672a\u8655\u7406\u7684\u53ef\u9078\u578b\u5225\u7b49\u554f\u984c\u3002<\/p>\n<h3>2. \u70ba\u4ec0\u9ebc\u8981\u4f7f\u7528 <code>Codable<\/code> \u800c\u4e0d\u662f <code>JSONSerialization<\/code>\uff1f<\/h3>\n<p><code>Codable<\/code> \u63d0\u4f9b\u4e86\u4e00\u500b\u66f4\u7c21\u6f54\u4e14\u5b89\u5168\u7684\u65b9\u5f0f\u4f86\u89e3\u6790\u8207\u751f\u6210 JSON\uff0c\u80fd\u5920\u81ea\u52d5\u8655\u7406\u578b\u5225\u8f49\u63db\u4e26\u6e1b\u5c11\u624b\u52d5\u89e3\u6790\u7684\u932f\u8aa4\u3002<\/p>\n<h3>3. \u5982\u4f55\u5f9e API \u7372\u53d6 JSON \u8cc7\u6599\u4e26\u89e3\u6790\uff1f<\/h3>\n<p>\u53ef\u4ee5\u4f7f\u7528 <code>URLSession<\/code> \u9032\u884c\u7db2\u8def\u8acb\u6c42\uff0c\u7372\u53d6 JSON \u8cc7\u6599\u5f8c\uff0c\u4f7f\u7528 <code>Codable<\/code> \u6216 <code>JSONSerialization<\/code> \u9032\u884c\u89e3\u6790\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Swift\u4e2d\u7684JSON\u8cc7\u6599\u89e3\u6790\uff0c\u53ef\u4ee5\u8b93\u4f60\u66f4\u6709\u6548\u7387\u5730\u8655\u7406\u8cc7\u6599\uff0c\u4e26\u66f4\u5feb\u5730\u5b8c\u6210\u4f60\u7684\u5de5\u4f5c\u3002\u672c\u6587\u5c07\u4ecb\u7d39Swift\u4e2d\u7684JSON\u8cc7\u6599\u89e3\u6790\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u8655\u7406\u8cc7\u6599\u3002<\/p>\n","protected":false},"author":1,"featured_media":7063,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[174,5],"tags":[173,9],"class_list":["post-7065","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-JSON-\u8cc7\u6599\u89e3\u6790-\ud83d\udcbb_1675401700.305016.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1PX","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7065","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=7065"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7065\/revisions"}],"predecessor-version":[{"id":13270,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7065\/revisions\/13270"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7063"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}