Arrays 如何在swift中将JSON数据附加到数组中

Arrays 如何在swift中将JSON数据附加到数组中,arrays,json,swift,Arrays,Json,Swift,大家好,我需要帮助,我正在尝试将json数据保存在数组中,但我没有正确地获取它。有人能帮我吗?这是完整的代码 let url = URL(string: "http://localhost:3000/liveData/device/20042") URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in if(error != nil){

大家好,我需要帮助,我正在尝试将json数据保存在数组中,但我没有正确地获取它。有人能帮我吗?这是完整的代码

let url = URL(string: "http://localhost:3000/liveData/device/20042")
    URLSession.shared.dataTask(with: url!, completionHandler: {
        (data, response, error) in
        if(error != nil){
            print("error")
        }else{
            do{
                let json = try JSONSerialization.jsonObject(with: data!, options:[]) as! [[String: Any]]

                print(json)

                    for item in json {

                    if let title = item["BV"] as? String {
                        self.userIdArray.append(title)
                    }

                    if let title = item["BC"] as? String {
                        self.userIdArray.append(title)
                    }

                    if let title = item["SV"] as? String {
                        self.userIdArray.append(title)
                    }

                    if let title = item["SC"] as? String {
                        self.userIdArray.append(title)
                    }

                }

                DispatchQueue.main.async {
                    self.collectionView.reloadData()
                }

            }catch let error as NSError{
                print(error)
            }
        }
    }).resume()
我想在userIdArray中保存json数据,任何人都可以帮助我,谢谢

let url = URL(string: "http://localhost:3000/liveData/device/20042")
    URLSession.shared.dataTask(with: url!, completionHandler: {
        (data, response, error) in
        if(error != nil){
            print("error")
        }else{
            do{
                let json = try JSONSerialization.jsonObject(with: data!, options:[]) as! [[String: Any]]

                print(json)

                    for item in json {

                    if let title = item["BV"] as? String {
                        self.userIdArray.append(title)
                    }

                    if let title = item["BC"] as? String {
                        self.userIdArray.append(title)
                    }

                    if let title = item["SV"] as? String {
                        self.userIdArray.append(title)
                    }

                    if let title = item["SC"] as? String {
                        self.userIdArray.append(title)
                    }

                }

                DispatchQueue.main.async {
                    self.collectionView.reloadData()
                }

            }catch let error as NSError{
                print(error)
            }
        }
    }).resume()
{
"SV" : 0,
"SC" : 0,
"BV" : 14.807,
"BC" : 0.024,
}

这是输出json

打印json后得到的是什么?“但我没有得到正确的结果”:这是怎么回事?你的JSON是什么(你做了
打印(JSON)
,输出是什么)?如果您不能告诉我们预期输出和当前输出是什么,我们如何帮助您了解问题所在?查看编辑后:
if let title=item[“SC”]as?字符串
不应该工作,因为
as?字符串
。你有双重价值。相反:
如果让title=item[“SC”]as?Double{self.userIdArray.append(String(title)}}
?哦,是的,没错,非常感谢@Larme
let url = URL(string: "http://localhost:3000/liveData/device/20042")
    URLSession.shared.dataTask(with: url!, completionHandler: {
        (data, response, error) in
        if(error != nil){
            print("error")
        }else{
            do{
                let json = try JSONSerialization.jsonObject(with: data!, options:[]) as! [[String: Any]]

                print(json)

                    for item in json {

                    if let title = item["BV"] as? String {
                        self.userIdArray.append(title)
                    }

                    if let title = item["BC"] as? String {
                        self.userIdArray.append(title)
                    }

                    if let title = item["SV"] as? String {
                        self.userIdArray.append(title)
                    }

                    if let title = item["SC"] as? String {
                        self.userIdArray.append(title)
                    }

                }

                DispatchQueue.main.async {
                    self.collectionView.reloadData()
                }

            }catch let error as NSError{
                print(error)
            }
        }
    }).resume()