将json解析为swift 3中的数组 [英] parsing json into an array in swift 3

查看:86
本文介绍了将json解析为swift 3中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是swift的新手,我从请求中得到一个json,但我无法解析。我正在尝试获取json信息并创建要在带有注释的mapkit上使用的坐标

I am new on swift and I am getting a json back from a request but I can not parse. I am trying to get the json info and create coordinates to use on mapkit with annotations as well

以下是我回来的json

Below is the json I get back

{
    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";
        }
    ] }

我尝试解析的代码是这个

and my code to try to parse is this

 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()

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

but not that I try works. I want to get the latitude, longitude and create an annotationn on the map

感谢您的帮助

推荐答案

您可以尝试使用与@Niko Adrianus Yuwono相同的代码,但进行了一些更改,因此您将获得teamid作为整数

You can try with below code its same as @Niko Adrianus Yuwono but made some changes so you will get teamid as integer

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

这篇关于将json解析为swift 3中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆