【问题标题】:How to parse and array of arrays with arrays in SwiftyJSON?如何使用 SwiftyJSON 中的数组解析数组和数组?
【发布时间】: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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2016-09-30
    • 1970-01-01
    • 2015-01-22
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多