如何解析以数组开头的数据[?

时间:2017-03-29 18:09:10

标签: json parsing swift3

所以在我的swift 3 xcode项目中,我想通过使用名为" http://tour-pedia.org/api/"的Web服务来使用JSOmN解析一些数据。 例如,其中的数据设置如下:

[
    {
     "address": "Science Park 904",
     "category": "attraction",
     "id": 30884,
     "lat": 52.355320008998,
     "lng": 4.9574317242814,
     "location": "Amsterdam",
     "name": "Dakterras Science Park",
     "originalId": "4d8b3370bc848cfa1043ea2b",
     "polarity": 0,
     "subCategory": "Scenic Lookout",    
    },
   {
     "address": "Science Park 201",
     "category": "attraction",
     "id": 30661,
     "lat": 52.356701093273,
     "lng": 4.9529844809109,
     "location": "Amsterdam",
     "name": "In 'de Natuur' rondom Science Park",
     "originalId": "4da2ede7c6e96ea85e1ede5d",
     "polarity": 0,
     "subCategory": "Field",  
     }
]

我想使用地址,位置和名称字段。

到目前为止,我已经这样做了:

func fetchInfos(){

    let urlRequest = URLRequest(url: URL(string: "http://tour-pedia.org/api/")!)
    let task = URLSession.shared.dataTask(with: urlRequest){ (data,response,error) in

        if error != nil {
            print(error)
            return
        }

        do{

            let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject]


        } catch let error {
            print(error)
        }

    }

}

但我不知道如何从这里继续。

任何帮助将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:-1)

让我们假设您的字符串s是带有sqaure括号的json String s =" [{\" a \":\" b \"}]&#34 ;;

s = s.substring(1,s.length() - 1); //现在进行所有处理,因为sqaure括号将被移除!!