如何在 SwiftyJSON 中解析数组和数组数组?
How to parse and array of arrays with arrays in SwiftyJSON?
我有一个 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))
}
}
}
}
我得到的错误是
正如 Pratik 在评论中提到的那样,我通过
解决了它
if let coordinatesArray: Array = geometry[RMJSONKey.coordinates].array {
for coordinates: JSON in coordinatesArray {
var polygonCoordinates: [CLLocationCoordinate2D] = []
for coordinatePair: (String, JSON) in coordinates {
let coordinateSet: JSON = coordinatePair.1
guard let latitude: Double = coordinateSet.arrayValue.last?.double,
let longitude: Double = coordinateSet.arrayValue.first?.double else {
throw RMException.invalidFormat
}
polygonCoordinates.append(CLLocationCoordinate2DMake(latitude, longitude))
}
}
}
我有一个 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))
}
}
}
}
我得到的错误是
正如 Pratik 在评论中提到的那样,我通过
解决了它if let coordinatesArray: Array = geometry[RMJSONKey.coordinates].array {
for coordinates: JSON in coordinatesArray {
var polygonCoordinates: [CLLocationCoordinate2D] = []
for coordinatePair: (String, JSON) in coordinates {
let coordinateSet: JSON = coordinatePair.1
guard let latitude: Double = coordinateSet.arrayValue.last?.double,
let longitude: Double = coordinateSet.arrayValue.first?.double else {
throw RMException.invalidFormat
}
polygonCoordinates.append(CLLocationCoordinate2DMake(latitude, longitude))
}
}
}