如何从 Swift 4.2 中的数组获取字典
How do I get a dictionary from an array in Swift 4.2
我见过很多类似的问题,但 none 似乎符合我的用例。
我有一个 json 文件结构如下:
{
"Trains": [{
"Car": "8",
"Destination": "Glenmont",
"DestinationCode": "B11",
"DestinationName": "Glenmont",
"Group": "1",
"Line": "RD",
"LocationCode": "A06",
"LocationName": "Van Ness-UDC",
"Min": "3"
}, {
"Car": "6",
"Destination": "Shady Gr",
"DestinationCode": "A15",
"DestinationName": "Shady Grove",
"Group": "2",
"Line": "RD",
"LocationCode": "A06",
"LocationName": "Van Ness-UDC",
"Min": "3"
}]
}
我正在尝试获取每列火车的字典。我已经尝试过这个(在其他努力中),但我无法理解它。这是我的代码:
jsonArray = [try! JSONSerialization.jsonObject(with: data!, options: .mutableContainers)] as! [String]
for train in jsonArray {
print(train["name"])
}
这无法编译。
我的json数组设置为:
var jsonArray = [Any]()
我希望这个答案符合您的情况,请在下方查看,
不要混淆我在文件中使用了您的 JSON 回复。
if let path = Bundle.main.path(forResource: "file", ofType: "json") {
do {
let data1 = try Data(contentsOf: URL(fileURLWithPath: path), options: [])
let jsonDic = try JSONSerialization.jsonObject(with: data1, options: .mutableContainers) as? [String:Any]
guard let dic = jsonDic else { return}
if let dict = dic["Trains"] as? [[String:Any]]{
print(dict)
}
} catch {
print(error as NSError)
}
}
如果你想使用解码器,那么使用这个。
struct Result: Decodable {
let Trains:[transaction]
}
struct transaction: Decodable {
let Car:String
let Destination:String
let DestinationCode:String
}
var result = [Result]()
if let path = Bundle.main.path(forResource: "file", ofType: "json") {
do {
let data1 = try Data(contentsOf: URL(fileURLWithPath: path), options: [])
let decoder = JSONDecoder()
result = [try decoder.decode(Result.self, from: data1)]
print(result)
} catch {
print(error)
}
}
如果我的编码有任何错误,欢迎指出。
我见过很多类似的问题,但 none 似乎符合我的用例。
我有一个 json 文件结构如下:
{
"Trains": [{
"Car": "8",
"Destination": "Glenmont",
"DestinationCode": "B11",
"DestinationName": "Glenmont",
"Group": "1",
"Line": "RD",
"LocationCode": "A06",
"LocationName": "Van Ness-UDC",
"Min": "3"
}, {
"Car": "6",
"Destination": "Shady Gr",
"DestinationCode": "A15",
"DestinationName": "Shady Grove",
"Group": "2",
"Line": "RD",
"LocationCode": "A06",
"LocationName": "Van Ness-UDC",
"Min": "3"
}]
}
我正在尝试获取每列火车的字典。我已经尝试过这个(在其他努力中),但我无法理解它。这是我的代码:
jsonArray = [try! JSONSerialization.jsonObject(with: data!, options: .mutableContainers)] as! [String]
for train in jsonArray {
print(train["name"])
}
这无法编译。
我的json数组设置为:
var jsonArray = [Any]()
我希望这个答案符合您的情况,请在下方查看, 不要混淆我在文件中使用了您的 JSON 回复。
if let path = Bundle.main.path(forResource: "file", ofType: "json") {
do {
let data1 = try Data(contentsOf: URL(fileURLWithPath: path), options: [])
let jsonDic = try JSONSerialization.jsonObject(with: data1, options: .mutableContainers) as? [String:Any]
guard let dic = jsonDic else { return}
if let dict = dic["Trains"] as? [[String:Any]]{
print(dict)
}
} catch {
print(error as NSError)
}
}
如果你想使用解码器,那么使用这个。
struct Result: Decodable {
let Trains:[transaction]
}
struct transaction: Decodable {
let Car:String
let Destination:String
let DestinationCode:String
}
var result = [Result]()
if let path = Bundle.main.path(forResource: "file", ofType: "json") {
do {
let data1 = try Data(contentsOf: URL(fileURLWithPath: path), options: [])
let decoder = JSONDecoder()
result = [try decoder.decode(Result.self, from: data1)]
print(result)
} catch {
print(error)
}
}
如果我的编码有任何错误,欢迎指出。