从GeoJSON中提取数据

时间:2018-02-13 17:09:19

标签: ios json swift geojson

我正在尝试检索下面GeoJSON中的要素类。

我已经更新了下面的GeoJSON。

{
"type": "FeatureCollection",
"features": [
    {
    "type": "Feature",
    "properties": {
             "scalerank": 8,
             "name": "Grill",
             "website": "www.rocargo.com/SanNicolas.html",
             "natlscale": 5,
             "featureclass": "Meat"
             },
    "geometry": {
            "type": "Point",
            "coordinates": [-11.1086263, 59.1438153]
            }
    },
    {
    "type": "Feature",
    "properties": {
            "scalerank": 8,
             "name": "Queen Vic",
             "website": "www.rocargo.com/SanNicolas.html",
             "natlscale": 5,
             "featureclass": "Fish"
             },
    "geometry": {
            "type": "Point",
             "coordinates": [-11.1190539, 59.1498404]
             }
    },
    {
    "type": "Feature",
    "properties": {
            "scalerank": 8,
             "name": "Josephines",
             "website": "www.rocargo.com/SanNicolas.html",
             "natlscale": 5,
             "featureclass": "Bar"
             },
    "geometry": {
            "type": "Point",
             "coordinates": [-11.1145087,59.142496]
             }
    },
    {
    "type": "Feature",
    "properties": {
             "scalerank": 8,
             "name": "Fall",
             "website": "www.rocargo.com/SanNicolas.html",
             "natlscale": 5,
             "featureclass": "Port"
             },
    "geometry": {
            "type": "Point",
            "coordinates": [-11.1174109, 59.1402164]
            }        
    }
    ]
}

以下功能可以提取上述所有信息。

   func pleaseWork() {

    let urlBar = Bundle.main.path(forResource: "bars", ofType: "geojson")!

    if let jsonData = NSData(contentsOfFile: urlBar) {
        do {
            if let jsonResult: NSDictionary = try JSONSerialization.jsonObject(with: jsonData as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary {

                if let responseA : NSArray = jsonResult["features"] as? NSArray {

                        print(responseA)



                }
            }
        }
        catch { print("Error while parsing: \(error)") }

    }

我可以提取所有信息,但我正在努力获取'featureclass'信息。我错过了哪些步骤?

感谢。 asdfadsfadsfdsafdsafdsfadsfdsafdsafdasf asdfasdfadsfdsa

2 个答案:

答案 0 :(得分:3)

我建议在Swift 4中使用Decodable。它非常简单方便

创建结构

struct Collection : Decodable {
    let type : String
    let features : [Feature]
}

struct Feature : Decodable {
    let type : String
    let properties : Properties
    // there is also geometry
}

struct Properties : Decodable {
    let scalerank : Int
    let name : String
    let website : URL
    let natlscale : Int
    let featureclass : String
}

解码数据并打印namefeatureclass

的值
let urlBar = Bundle.main.url(forResource: "bars", withExtension: "geojson")!

do {
    let jsonData = try Data(contentsOf: urlBar)
    let result = try JSONDecoder().decode(Collection.self, from: jsonData)
    for feature in result.features {
        print("name", feature.properties.name, "featureclass", feature.properties.featureclass)
    }
} catch { print("Error while parsing: \(error)") }

答案 1 :(得分:1)

只有一步一步,你才能做到这一点。

if let responseA : NSArray = jsonResult["features"] as? NSArray {

   for dictVal in 0..<responseA.count
   {
       let featuresDict = responseA[dictVal] as! NSDictionary
       let propertiesDict = featuresDict.value(forKey: "properties") as! NSDictionary
       let featureClassName = propertiesDict.value(forKey: "featureclass") as! String
       print(featureClassName)
   }

}

尝试使用this链接进行复杂JSON验证。你会明白的。

enter image description here