我从网页上获得了以下Json数据
[{"time": "2019-02-04", "lat": 0.0, "lng": 0.0, "base_stations": ["ABCD"], "best_server": "6652", "avg_rssi": -84.67605633802818, "reception_count": 213.0, "link_quality": "GOOD"}, {"time": "2019-02-07", "lat": 90.71, "lng": 100.64, "base_stations": ["CDEF", "XPTO", "ZZEEG"], "best_server": "YYYY", "avg_rssi": -133.0, "reception_count": 3.0, "link_quality": "EXCELLENT"},{....},{....}]
我想对其进行解码。我在想这样的事情:
let DamasDriveAddress_Grid = "http://bbbba.fff.com/json_pg"
let url_Grid = URL(string: DamasDriveAddress_Grid)!
let jsonData_Grid = try! Data(contentsOf: url_Grid)
struct Test_Grid: Codable {
let time: Date
let lat: Double
let lng: Double
let base_stations: [Array]
let best_server:String
let avg_rssi:Double
let reception_count: Double
let link_quality: String
}
let jsonDecoder_Grid = JSONDecoder()
let Grid = try? jsonDecoder_Grid.decode(Array<Test_Grid>.self,
from: jsonData_Grid)
但这不起作用。
有人可以帮我吗?
谢谢
答案 0 :(得分:0)
base_stations
应该是[String]
struct Test_Grid: Codable {
let time: Date
let lat: Double
let lng: Double
let base_stations: [String] <-- here
let best_server:String
let avg_rssi:Double
let reception_count: Double
let link_quality: String
}
此外,您还应该指定一种日期解码策略:
let jsonDecoder_Grid = JSONDecoder()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
jsonDecoder_Grid.dateDecodingStrategy = .formatted(formatter)