LoginSignup
9

More than 5 years have passed since last update.

「JSON.swift」JSON取り扱いライブラリ、シンブルで便利

Last updated at Posted at 2017-02-16

SwiftyJSON.swiftのファイルの長さに驚いたので、自分で作ってみました。

JSON ←ここ

let json = JSON(data: someData)

let json = JSON(object)

Subscript

// [JSON]?
let users = json["users"]

// JSON?
let user = users?[0]

// String?
let name = user?["name"]

// NSNumber?
let age = user?["age"].number

// Bool?
let bool = user?["married"].bool

// one liner
let name = json["users"]?[0]?["name"]?.string


// for in
if let users = json["users"].array {
    for user: JSON? in users {

    }
}

if let user = json["users"]?[0].dictionary {
    for (key, value) in user {

    }
}


// String
json.prettyPrinted()

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9