JSON数据: A_Geo: "MULTIPOINT ((121.5831587 24.9971109))" need to be parsed into dictionary

标签 json swift dictionary

对于下面的[key:value]对,不太好用

A_Geo:“MULTIPOINT((121.5831587 24.9971109))”

我需要支持将其转换为字典并像其他对一样组合成字典数组

[ _id:1,.....,经纬度:121.5831587,纬度:24.9971109,......]

为了解析以下 JSON 以将地理数据分析为 lng 和 lat,我们尝试应用 JSONSerialization。但不知道更进一步。

result: 
{
        offset: 0,
        limit: 10000,
        results:[
     -{
        _id: "1",
        A_Name_Ch: “Great Hotel Spot",
        A_Geo: "MULTIPOINT ((121.5831587 24.9971109))",
        A_Location: “Great Spot",
        A_Name_En: "Giant Great",
        A_CID: "1"
    }, {…}, {…}
    }

    ]
}

let ArticlesUrl = URL (string: "http://data.taipei/opendata/datalist/apiAccess?scope=resourceAquire&rid=a3e2b221-75e0-45c1-8f97-75acbd43d613")!
let session = URLSession.shared
    let task = session.dataTask(with: ArticlesUrl){(data,response,error) in
        if let error = error {
            print ("failed!!")
            completionHandler(nil,error)
            return
        }
        let data = data!

        if let jsonObject = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as![String:[String: Any]], let articleArray = jsonObject["result"]!["results"] as? [[String: Any]]{

            var articles = [Article]()
            for articleDict in articleArray{
                let article = Article(rawData: articleDict)
                articles.append(article)
            }
            completionHandler(articles, nil)
        }
    }
    task.resume()

最佳答案

解析 MULTIPOINT 字符串表达式的替代方法是 Scanner

let string = "MULTIPOINT ((121.5831587 24.9971109))"
let scanner = Scanner(string: string)
var lat = 0.0, lng = 0.0
if scanner.scanUpToCharacters(from: CharacterSet.decimalDigits, into: nil) {
    scanner.scanDouble(&lat)
    scanner.scanDouble(&lng)
}
let coordinate = ["lat" : lat, "lng" : lng]

关于JSON数据: A_Geo: "MULTIPOINT ((121.5831587 24.9971109))" need to be parsed into dictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50637161/

相关文章:

ios - 尝试更新单元格时,自定义 CollectionView 单元格的布局元素消失

ios - 根据 UITextView 属性文本计算 UICollectionViewCell 高度

python - 比较两个字典中的键并使用 for 循环更新值

ios - 如何在 swift 中发送带有嵌套字典的 POST 请求

android - 从电子邮件 header 解析 JSON

c# - 如何将 JSON 格式的数据接收到 C# 方法参数中

swift - 将图像添加到 UIButton 使其不可点击

hadoop - Hive:如何构建 UDTF?

dictionary - erlang dict的时间复杂度

php - Volley 没有回应