Code: Select all
func jsonFour() {
let string = "[{\"address\": 7023000630,\"reportStatus\": \"Retrieved\",\"currentLocation\": {\"latitude\": 29.8529, \"longitude\": 73.99332,\"timestamp\": \"2019-01-07T16:35:25.079+05:30\"} }, {\"address\": 7290098339, \"reportStatus\": \"Retrieved\", \"currentLocation\": {\"latitude\": 21.628569, \"longitude\": 72.996956,\"timestamp\": \"2019-01-07T16:35:25.079+05:30\" } } ]"
let data = string.data(using: .utf8)!
do {
if let jsonArray = try JSONSerialization.jsonObject(with: data, options : JSONSerialization.ReadingOptions.mutableContainers) as? [[ String : Any ]]
{
print(jsonArray) // use the json here
let address = jsonArray["address"] as! [[String:Any]]
if let timestamp = address["timestamp"] as? [String]{print(timestamp)}
} else {
print("bad json")
}
} catch let error as NSError {
print(error)
}
}
"bad json".
Was mache ich hier falsch?