【问题标题】:Parsing JSON in Swfit 3.0 Cast NSArray to NSDictionary Error在 Swfit 3.0 中解析 JSON 将 NSArray 转换为 NSDictionary 错误
【发布时间】:2017-02-13 20:30:24
【问题描述】:

我正在尝试将 JSON Url 解析为 UICollectionView,但我不断收到无法将类型“__NSArrayI”(0x1010ebd88)的值转换为“NSDictionary”(0x1010ec288)。当我尝试加载数据时出现错误。我将 xcode 8 与 Swift 3.0 一起使用。任何人都可以看到我遇到的错误以及如何解决它?不知道从这里去哪里。

    let link = "http://www.flickr.com/services/feeds/photos_public.gne?tags=swimming&format=json&nojsoncallback=1"

    let urlString = link

    let url = URL(string: urlString)
    URLSession.shared.dataTask(with:url!) { (data, response, error) in
        if error != nil {
            print(error)
        } else {
            do {

                let parsedData = try JSONSerialization.jsonObject(with: data!, options: []) as! [String:Any]
                let currentConditions = parsedData["items"] as! [String:Any]

                print(currentConditions)

                let currentItem = currentConditions["link"] as! [String:Any]
                print(currentItem)

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

        }.resume()

编辑:好的所以我已经走了这么远,现在在“项目”中,我需要将另一个字符串提取到一个数组中。在项目中,有几个“链接=”我需要一个数组中的链接,但不知道如何把它拿出来。当我打印键值时,项目会显示出来,但它似乎只是一个长字符串,而不是在数组或字典中。有什么建议吗?谢谢,

            let urlString = link
    var itemArray: Any?
    var parsedData: [String:Any] = ["":""]

    let url = URL(string: urlString)
    URLSession.shared.dataTask(with:url!) { (data, response, error) in
        if error != nil {
            print(error)
        } else {
            do {

                parsedData = try JSONSerialization.jsonObject(with: data!, options: []) as! [String:Any]
                itemArray = parsedData["items"] as! [[String:Any]]



                for (key, value) in parsedData {
                    print("\(key) - \(value) ")
                }



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

        }.resume()

    guard let images_array = parsedData["items"] as? NSArray else
    {
        print("error2")
        return
    }

【问题讨论】:

  • 在您的 JSON 中,parsedData["items"] 是一个数组,而不是字典,因此您应该将其转换为 [[String:Any]]currentConditions["link"] 也不是字典,它是 String
  • OK 这有助于打印 currentConditions。现在,即使我将它设置为字符串,它也不允许我从 currentConditions 中拉出“链接”。我更新的代码让 currentItem = currentConditions["link"] as!字符串现在正在发送错误,无法使用“字符串”类型的索引为 [[String:Any]] 类型的值下标

标签: json swift


【解决方案1】:

你可以使用这个: 由于项目中的数据是字典数组,而链接不在内部数组中,它是主字典,因此您可以通过将链接作为键传递来轻松获取它。

 let link = "http://www.flickr.com/services/feeds/photos_public.gne?tags=swimming&format=json&nojsoncallback=1"

        let urlString = link

        let url = URL(string: urlString)
        URLSession.shared.dataTask(with:url!) { (data, response, error) in
            if error != nil {
                print(error)
            } else {
                do {

                    let parsedData = try JSONSerialization.jsonObject(with: data!, options: []) as! [String:Any]
                    let currentConditions = parsedData["items"] as! [[String:Any]]

                    print(currentConditions)

                    let currentItem = parsedData["link"] as! String
                    print(currentItem)

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

            }.resume()

【讨论】:

    【解决方案2】:

    如cmets中提到的parsedData["items"]是一个数组,而键link的值是一个字符串,所以你需要使用循环:

    typealias JSONDictionary = [String:Any] // for convenience
    
    ...
    
    if let parsedData = try JSONSerialization.jsonObject(with:data!, options: []) as? JSONDictionary {
            // print(jsonData)
            guard let currentConditions = parsedData["items"] as? [JSONDictionary] else { return }
            for condition in currentConditions {
                print(condition["link"] as! String)
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-27
      • 1970-01-01
      • 2015-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多