Foundationに含まれているJSONSerializationを使う
JSON文字列をパースするには、JSONSerialization.jsonObjectを使用する。
JSONSerializationJ .jsonObject(with JSONデータ: Data, options オプション)
import Foundation import CryptoSwift let jsonString: String = "{\"id\":3932, \"company\":\"Akatsuki\"}" var personalData: Data = jsonString.data(using: String.Encoding.utf8)! do { let items = try JSONSerialization.jsonObject(with: personalData) as! Dictionary<String,Any> print(items["id"] as! Int) print(items["company"] as! String) } catch { print(error) }
3932
Akatsuki
書き方を変える。
let jsonString: String = "{\"id\":3932, \"company\":\"Akatsuki\"}" var personalData: Data = jsonString.data(using: String.Encoding.utf8)! let items = try JSONSerialization.jsonObject(with: personalData) as! Dictionary<String,Any> var code:Int = items["id"] as! Int print(code)
3932
ではXcodeに当てはめましょう。
そもそも、import Foundationって、xcodeに使えるのか?
swift foundation
https://github.com/apple/swift-corelibs-foundation
The Foundation framework defines a base layer of functionality that is required for almost all applications. It provides primitive classes and introduces several paradigms that define functionality not provided by either the Objective-C runtime and language or Swift standard library and language.
よくわかりませんが、やってみましょう。