ios – 在swift 3中将json解析为数组

ios – 在swift 3中将json解析为数组,第1张

概述我是 swift的新手,我从请求中得到一个json,但我无法解析.我正在尝试获取json信息并创建在mapkit上使用注释的坐标 下面是我回来的json { coord = [ { islocationactive = 1; latitude = "37.8037522"; lo 我是 swift的新手,我从请求中得到一个Json,但我无法解析.我正在尝试获取Json信息并创建在mapkit上使用注释的坐标

下面是我回来的Json

{    coord =     [                {            islocationactive = 1;            latitude = "37.8037522";            locationID = 1;            locationsubTitle = Danville;            locationTitle = "Schreiner's Home";            longitude = "121.9871216";        },{            islocationactive = 1;            latitude = "37.8191921";            locationID = 2;            locationsubTitle = "Elementary School";            locationTitle = Montair;            longitude = "-122.0071005";        },{            islocationactive = 1;            latitude = "37.8186077";            locationID = 3;            locationsubTitle = "Americas Eats";            locationTitle = "Chaus Restaurant";            longitude = "-121.999046";        },{            islocationactive = 1;            latitude = "37.7789669";            locationID = 4;            locationsubTitle = "Cheer & Dance";            locationTitle = Valley;            longitude = "-121.9829908";        }    ] }

我尝试解析的代码就是这个

let task = URLSession.shared.dataTask(with: request as URLRequest){            data,response,error in            //exiting if there is some error            if error != nil{                print("error is \(error)")                return;            }            //parsing the response            do {                //converting resonse to NSDictionary                var teamJsON: NSDictionary!                teamJsON =  try JsONSerialization.JsonObject(with: data!,options: .mutableContainers) as? NSDictionary                print(teamJsON)                //getting the JsON array teams from the response                let liquIDLocations: NSArray = teamJsON["coord"] as! NSArray                //looPing through all the Json objects in the array teams                for i in 0 ..< liquIDLocations.count{                    //getting the data at each index      //              let teamID:Int = liquIDLocations[i]["locationID"] as! Int!                }            } catch {                print(error)            }        }        //executing the task        task.resume()

但不是我尝试工作.我想得到纬度,经度并在地图上创建一个注释

谢谢您的帮助

解决方法 您可以尝试使用与@Niko Adrianus Yuwono相同的代码,但进行了一些更改,因此您将获得teamID作为整数
do {        let data : NSData = NSData() // change your data variable as you get from webservice response        guard let teamJsON =  try NSJsONSerialization.JsONObjectWithData(data,options: []) as? [String: Any],let liquIDLocations = teamJsON["coord"] as? [[String: Any]]            else { return }        //looPing through all the Json objects in the array teams        for i in 0 ..< liquIDLocations.count{            let teamID: Int = (liquIDLocations[i]["locationID"] as! Nsstring).integerValue            print(teamID)        }    } catch {        print(error)    }
总结

以上是内存溢出为你收集整理的ios – 在swift 3中将json解析为数组全部内容,希望文章能够帮你解决ios – 在swift 3中将json解析为数组所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1106321.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存