ios - 如何在 SwiftyJSON 中解析数组和数组数组?

标签 ios json swift swifty-json

我有一个如下所示的 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))
                            }
                        }
                    }
                }

我得到的错误是 enter image description here

最佳答案

正如 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))
       }
   }
}

关于ios - 如何在 SwiftyJSON 中解析数组和数组数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51262733/

相关文章:

ios - 如何在点击时更改 UITableViewCell 的部分位置

IOS CNContactStore executeSaveRequest 失败,CNErrorDomain Code=500

iphone - 带有 TextField 的 UITableViewCell 并检查输入

ios - 在完成处理程序中的 http 状态代码上隐藏进度条 - Swift 4

ios - UITextView 中的 HTML 格式

json - 使用 React DropZone 将 CSV 转换为 JSON 客户端

javascript - 同步跨域AJAX请求

iphone - 从iphone sdk中的uiwebview下载文件

php - 以 json 格式传递数据时出现无效属性 ID 错误

xcode - 使用 Swift 列出 iTunes 共享文件夹中的文件