ios - 从 GeoJSON 中提取数据

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

    }

我可以提取所有信息,但是,我正在努力获取“要素类”信息。我缺少哪些步骤?

谢谢。 asdfadsfadsfdsafdsafdsfadsfdsafdsafdasf asdfasdfadsfdsa

最佳答案

我推荐在 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)") }

关于ios - 从 GeoJSON 中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48772086/

相关文章:

ios - 将 NSDate 从一个时区更改为另一个时区

ios - 在 swift 文件之间共享一个变量

ios - SWIFT TableView 错误

ios - 给表情符号着色 "image"

ios - Swift 无法将类型 __NSInlineData 的值转换为 PFFile

json - 快速访问 JSON 数据

ios - 如何将视频文件保存到文档目录

java - 如何在 TextView 中设置格式化的 JSON 响应字符串?

python - 哪个是压缩 json 以存储在基于内存的存储(如 redis 或 memcache)中的最佳方式?

ios - 搜索栏未显示在导航栏 iOS 11 上