从 GeoJSON 中提取数据

Extract data from 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

只要循序渐进,你就可以做到。

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 link 进行复杂 JSON 验证。你会清楚的。

我在Swift中推荐使用Decodable 4.非常简单方便

创建结构

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