SWIFT4 JSON解析防火墙 我有一个解析的JSON,我如何从中获取电子邮件和用户名到变量?以及如何将城市名称从中获取到城市名称数组?
struct User: Codable {
let email: String
let username: String
let cities: [city]
}
struct city: Codable {
let cityname: String
}
if snapshot.exists()
{
print(snapshot)
guard let value = snapshot.value else { return }
do {
let parsedData = try FirebaseDecoder().decode([User].self, from: value)
print(parsedData)
} catch let error {
print(error)
}
}
JSON: Snap (users) {
0 = {
cinemas = {
0 = {
cityname = Kazan;
};
1 = {
cityname = Kursk;
};
2 = {
cityname = Volgograd;
};
};
email = "placeholder@gmail.com";
username = “Alex”;
};
}
答案 0 :(得分:1)
您可以尝试循环播放
parsedData.forEach {
print($0.email)
}
如果是1位用户,则
if ler user = parsedData.first {
print(user.email)
let cities = user.cities.map { $0.cityname }
print(cities)
}