【发布时间】:2018-12-18 03:39:06
【问题描述】:
我有如下 JSON 响应
{
"geometry" : {
"type" : "GeometryCollection",
"geometries" : [
{
"type" : "Polygon",
"coordinates" : [
[
[
43.7228393554688,
42.065249641381598
],
[
43.8217163085938,
42.084617318779898
],
[
43.899993896484403,
42.100922357518499
],
[
43.7228393554688,
42.065249641381598
]
]
]
}
]
}
我在上面发布的 JSON 是我的 JSON 响应的一部分。该数组提供以下格式的数据。
[[[Array_Polygon1_Coordinates1], [Array_Polygon1_Coordinates2],[Array_Polygon1_Coordinates3]], [[Array_Polygon2_Coordinates1], [Array_Polygon2_Coordinates2], [Array_Polygon2_Coordinates3]]]
我在我的项目中使用 SwiftyJSON。如何使用 SwiftyJSON 解析这个 JSON? 我的代码
guard let geometries: Array = event["geometry"]["geometries"].array else {
throw RMException.invalidFormat
}
for geometry: JSON in geometries {
if let coordinatesArray: Array = geometry["coordinates"].array {
for coordinates: JSON in coordinatesArray {
var polygonCoordinates: [CLLocationCoordinate2D] = []
for coordinatePair in coordinates {
guard let coordinatePair = coordinatePair.array,
let latitude: Double = coordinatePair.last?.double,
let longitude: Double = coordinatePair.first?.double else {
throw RMException.invalidFormat
}
polygonCoordinates.append(CLLocationCoordinate2DMake(latitude, longitude))
}
}
}
}
【问题讨论】:
-
显示你尝试过的代码
-
@Anbu.karthik 将我的代码添加到问题中
-
不要将代码添加为图像,超过您的代码和 json 响应
-
@PratikPrajapati 对不起。我现在更新了我的问题
-
错误表示
coordinatePair是元组,尝试coordinatePair.1获取值
标签: ios json swift swifty-json