gpt4 book ai didi

JSON 解包未运行

转载 作者:行者123 更新时间:2023-11-28 06:59:21 25 4
gpt4 key购买 nike

我进行了一次 JSON 提取,然后对数据进行处理。我的 JSONObject 已创建,然后我开始处理数据。示例可在此处查看:https://openlibrary.org/api/books?bibkeys=1593243987&f&jscmd=data&format=json

我提取作者姓名的第一个 block 工作正常,但是第二个提取封面 url 作为字符串的 block 甚至没有运行,我不知道为什么。

如果我在 处设置断点 if let thumbs = bookDictionary["cover"] as? NSArray {它停止了,但是当我“单步执行”代码时,它跳到最后并继续前进,甚至没有在 block 内运行任何东西。

如果有人能提供任何帮助,我将不胜感激。我正在使用 Swift 2.0/Xcode 7b6。

let requestURL = ("https://openlibrary.org/api/books?bibkeys=" + lookUpID + "&f&jscmd=data&format=json")
let url = NSURL(string: requestURL)
let req = NSURLRequest(URL: url!)
let dataTask = session.dataTaskWithRequest(req) {
(data, response, error) in
do {
let jsonObject = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary


if let bookDictionary: AnyObject = jsonObject!["\(self.lookUpID)"] {

// Retrieve the author name
var names = [String]()
if let authors = bookDictionary["authors"] as? NSArray {
for author in authors {
if let author = author as? NSDictionary,
let name = author["name"] as? String {
names.append(name)
}
}
}

// Retrieve cover url
var coverThumbURL: String = ""
if let thumbs = bookDictionary["cover"] as? NSArray {
// This code isn't running at all.
for thumb in thumbs {
if let thumb = thumb as? NSDictionary,
let thumbnail = thumb["medium"] as? String {
coverThumbURL = thumbnail
}
}
}
}

最佳答案

感谢您的帮助。我环顾四周并修复了铸件。

var coverThumbURL: String = ""
if let thumbs = bookDictionary["cover"] as? NSDictionary {
let thumbnail = thumbs.valueForKey("medium") as? String
coverThumbURL = thumbnail!
}

关于JSON 解包未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32439663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com