无法转换类型' __ NSSingleObjectArrayI'的值来自NSDictionary'

时间:2016-12-13 09:31:29

标签: ios json swift parsing

我正在尝试使用iTunes查找API检查我的应用程序的版本。我在解析响应时遇到问题。请找到代码

static func needsUpdate() -> Bool
{
    do {
        let infoDictionary = Bundle.main.infoDictionary
        let appID = infoDictionary?["CFBundleIdentifier"]
        let url:URL = URL(string: "http://itunes.apple.com/lookup?bundleId=\(appID!)")!
        let data = try Data(contentsOf: url)
        let lookup = try JSONSerialization.jsonObject(with:data, options: []) as! [String:AnyObject]

        print(lookup)

        let resultCount:Int = lookup["resultCount"] as! Int

        if (resultCount == 1)
        {
            var results = lookup["results"] as! [String:AnyObject]  // ***Error***

            if results.isEmpty
            {
                print(results)
            }
        }

    } catch
    {

    }
    return true
}

请让我知道如何解析此回复

1 个答案:

答案 0 :(得分:10)

错误消息清楚地表明let results = lookup["results"] as! [[String:Any]] 的值是数组

[String:Any]

并且考虑到Swift 3中的JSON字典是def export_csv(request): response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="product-inventory.csv"' writer = csv.writer(response) writer.writerow(['Product Name', 'STATUS']) products = Product.objects.all().values_list('name', 'status') for product in products: writer.writerow(product) return response