[iOS] parse json string in swift

Posted in :

我有一組 json 長的像醬子,要在 swift 裡 parse

{ “date”:[

{“server_1”:[“192.168″,”192.169”]},

{“server_2”:[“10.1″,”10.2”]}

]

}

parse 用 swift


            let data = myString.data(using: String.Encoding.utf8, allowLossyConversion: false)!

            let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])

            if let responseJSON = responseJSON as? [String: Any] {

                let jsonArray = responseJSON[“data”] as? [[String: Any]]

                for json in jsonArray! {

                    for (key, value) in json {

                        let addressJSON = json[key] as? [String]

                        for ips in addressJSON! {

                                print (“server host at:” + key)

                        }

                    }

                }

            }

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *