我有一組 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)
}
}
}
}