Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Swift中解析JSON时出错,[String:Any]赢得';行不通_Json_Swift_Xcode_Macos - Fatal编程技术网

在Swift中解析JSON时出错,[String:Any]赢得';行不通

在Swift中解析JSON时出错,[String:Any]赢得';行不通,json,swift,xcode,macos,Json,Swift,Xcode,Macos,我试图在Swift 3中解析,但它崩溃了 这是密码 do{ 让data1:Data=try!Data(contentsOf:NSURL(string:https://gist.githubusercontent.com/DesWurstes/00baf946bd6d27e7e9355bd6e9969230/raw/a0de898faea8ddedb11b0db516967d0666255633/gist.json)作为!URL) 让jsono=try JSONSerialization.json

我试图在Swift 3中解析,但它崩溃了

这是密码

do{
让data1:Data=try!Data(contentsOf:NSURL(string:https://gist.githubusercontent.com/DesWurstes/00baf946bd6d27e7e9355bd6e9969230/raw/a0de898faea8ddedb11b0db516967d0666255633/gist.json)作为!URL)
让jsono=try JSONSerialization.jsonObject(使用:data1,选项:[])作为![String:Any]
}抓住{
//这里不使用catch。
}
以下是我在它崩溃时遇到的错误:

Could not cast value of type '__NSArrayI' (0x7fffe9cb9c08) to 'NSDictionary' (0x7fffe9cba158).
它崩溃是因为并非数组的所有元素都是字符串。(JSON的根是一个数组。)

为防止碰撞,使用以下方法更换第三条线路是合适的:

let jsono = try JSONSerialization.jsonObject(with: data1, options: [])
但是,它的类型将是
Any
,我将无法用

let string = jsono["something"] as! [String: Any] // Type "Any" has no subscript members
并且此代码不会运行:

if let array = jsono as? [String: Any] {
   print("test") // Doesn't print
}
在尝试修复第一个代码中的错误时,我认为这段代码可以工作(因为它说不能将数组转换为字典):

但结果

Could not cast value of type '__NSDictionaryI' (0x7fffe9cba108) to 'NSString' (0x7fffea072f38).

那么我如何解析这个JSON呢?

看起来从服务器返回的JSON响应是一个数组,其中包含
[String:Any]
类型的字典,因此您可以执行以下操作:

if let array = jsono as? [[String: Any]] {
  print("test") // Will print

  for dictionary in array {
    print(dictionary["url"] as! String)
  }
}

这里您可以看到我编写的测试代码。

您有解析数组响应,所以您需要键入castjson as?[[String:Any]]。。
如果您的响应是命令式的,那么您需要像json那样解析?[String:Any]

您的JSON是一个数组。所以它不能是字典。所以它至少是
[Any]
(数组)。现在,如果我们看得更多,它是一个字典数组,其中键是字符串,因此它是
as[[String:Any]]
不错!你也试过了吗?这是一个不需要下载的游乐场,而且更容易测试。不,我还没有试过。但是谢谢你让我知道。
func Callservice()
{
    let jsonUrlString = "url"
    guard let url = URL(string: jsonUrlString) else { return }

    URLSession.shared.dataTask(with: url) { (data, response, err) in
        guard let data = data else { return }
        do {

            let courses = try JSONDecoder().decode([Course].self, from: data)
            self.arrayData = courses

            print(courses)


        } catch let jsonErr {
            print("Error serializing json:", jsonErr)
        }



        }.resume()
}



struct course:decodable{
let name:string?
}
func Callservice()
{
    let jsonUrlString = "url"
    guard let url = URL(string: jsonUrlString) else { return }

    URLSession.shared.dataTask(with: url) { (data, response, err) in
        guard let data = data else { return }
        do {

            let courses = try JSONDecoder().decode([Course].self, from: data)
            self.arrayData = courses

            print(courses)


        } catch let jsonErr {
            print("Error serializing json:", jsonErr)
        }



        }.resume()
}



struct course:decodable{
let name:string?
}