0

I have a JSON response like below

{
        "geometry" : {
          "type" : "GeometryCollection",
          "geometries" : [
            {
              "type" : "Polygon",
              "coordinates" : [
                [
                  [
                    43.7228393554688,
                    42.065249641381598
                  ],
                  [
                    43.8217163085938,
                    42.084617318779898
                  ],
                  [
                    43.899993896484403,
                    42.100922357518499
                  ],
                  [
                    43.7228393554688,
                    42.065249641381598
                  ]
                ]
              ]
            }
          ]
        }

The JSON I posted above is a part of my JSON response. This array gives data in the following format.

[[[Array_Polygon1_Coordinates1], [Array_Polygon1_Coordinates2],[Array_Polygon1_Coordinates3]], [[Array_Polygon2_Coordinates1], [Array_Polygon2_Coordinates2], [Array_Polygon2_Coordinates3]]]

I am using SwiftyJSON in my project. How can I parse this JSON using SwiftyJSON? My code

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

The error I get is enter image description here

9
  • show your tried code Commented Jul 10, 2018 at 10:22
  • @Anbu.karthik added my code to the question Commented Jul 10, 2018 at 10:31
  • don't add code as image, past your code and json response Commented Jul 10, 2018 at 10:34
  • @PratikPrajapati Sorry. I have updated my question now Commented Jul 10, 2018 at 10:45
  • Error indicates that coordinatePair is tuple , try coordinatePair.1 to get value Commented Jul 10, 2018 at 11:02

1 Answer 1

2

As Pratik mentioned in the comment I solved it by

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))
       }
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.