JSON字符串中出现意外的nil
我在应用程序中解析JSON,一些JSON具有我处理的JSON字符串中出现意外的nil,json,swift,null,Json,Swift,Null,我在应用程序中解析JSON,一些JSON具有我处理的nil值。但是,该应用程序仍然向我显示JSON包含nil值的错误。我的代码: struct Response : Decodable { let articles: [Article] } struct Article: Decodable { let title: String let description : String let url : String let urlToImage : String } URLSession.s
nil
值。但是,该应用程序仍然向我显示JSON包含nil
值的错误。我的代码:
struct Response : Decodable {
let articles: [Article]
}
struct Article: Decodable {
let title: String
let description : String
let url : String
let urlToImage : String
}
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else { return }
do {
let article = try JSONDecoder().decode(Response.self , from : data)
for i in 0...article.articles.count - 1 {
if type(of: article.articles[i].title) == NSNull.self {
beforeLoadNewsViewController.titleArray.append("")
} else {
beforeLoadNewsViewController.titleArray.append(article.articles[i].title)
}
if type(of : article.articles[i].urlToImage) == NSNull.self {
beforeLoadNewsViewController.newsImages.append(newsListViewController.newsImages[newsListViewController.newsIndex])
} else {
let url = URL(string: article.articles[i].urlToImage ?? "https://static.wixstatic.com/media/b77fe464cfc445da9003a5383a3e1acf.jpg")
let data = try? Data(contentsOf: url!)
if url != nil {
//make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
let img = UIImage(data: data!)
beforeLoadNewsViewController.newsImages.append(img!)
} else {
beforeLoadNewsViewController.newsImages.append(newsListViewController.newsImages[newsListViewController.newsIndex])
}
这是运行应用程序时出现的错误:
FC91DEEC1631350EFA71C9C561D).description],debugDescription:“应为字符串值,但发现为空。”,underlineError:nil))
注:
此JSON与其他没有nil
值的URL一起工作
这里是json
{
"status": "ok",
"source": "entertainment-weekly",
"sortBy": "top",
"articles": [
{
"author": null,
"title": "Hollywood's Original Gone Girl",
"description": null,
"url": "http://mariemcdonald.ew.com/",
"urlToImage": null,
"publishedAt": null
},
{
"author": "Samantha Highfill",
"title": "‘Supernatural’: Jensen Ackles, Jared Padalecki say the boys aren’t going ‘full-mope’",
"description": "",
"url": "http://ew.com/tv/2017/10/18/supernatural-jensen-ackles-jared-padalecki-season-13/",
"urlToImage": "http://ewedit.files.wordpress.com/2017/10/supernatural-season-13-episode-1.jpg?crop=0px%2C0px%2C2700px%2C1417.5px&resize=1200%2C630",
"publishedAt": "2017-10-18T17:23:54Z"
},
{
对不起,我还不能发表评论。但是,为什么不按照在URLSession数据检查时使用的方式测试“null” 然后看起来像:
guard let title = article.articles[i].title else {
beforeLoadNewsViewController.titleArray.append("")
return
}
beforeLoadNewsViewController.titleArray.append(title)
您尝试在这里解析JSON结构,如果JSON结构不适合您的需要,您仍然希望它具有相应的属性
你能提供你的JSON结构和你的解码方式吗
try JSONDecoder().decode(Response.self , from : data)
看起来?错误很明显
JSONDecoder
将NSNull
映射到nil
,因此如果解码器要将nil
值解码为非可选类型,则会抛出错误
解决方案是将所有受影响的属性声明为可选
let title: String
let description : String?
let url : String
let urlToImage : String?
或者自定义解码器,用空字符串替换nil
因为JSONDecoder
将NSNull
映射到nil
检查if…==NSNull.self{
是无用的
编辑:
不要使用丑陋的基于索引的C样式循环
let response = try JSONDecoder().decode(Response.self , from : data)
for article in response.articles {
beforeLoadNewsViewController.titleArray.append(article.title)
}
PS:但是,看在上帝的份上,你为什么要把文章实例映射到——显然是——单独的数组?你得到了
文章
实例,它们分别包含了与一篇文章相关的所有内容。你必须共享相关的JSON……你还需要什么呢?你正试图决定哪个JSON返回这个错误的JSON……新闻美联社我是娱乐周刊的JSON,请在你的帖子中包含JSON本身。我对其他URL没有任何问题,这儿子是新闻api,在娱乐周刊的api中,我有问题,因为儿子包含零值-还有一件事我使用了你的代码,但应用程序不允许我运行,因为guard let Line为什么不干脆beforeLoadNewsViewController.titleArray.append(article.articles[i].title???)
或使用mapbeforeLoadNewsViewController.titleArray=article.articles.map{$0.title???”}
不需要数组中的空字符串,请使用article.articles.flatMap{$0.title}
在加载NewsViewController.titleArray.append(article.artic)之前使用的标题les[i].title???”)但没有区别仍然存在问题,所以我使用了这个,但我将收到致命的Error@SaeedRahmatolahi在哪一行?beforeLoadNewsViewController.descArray.append(article.articles[i].description!)然后解码工作正常。titleArray
在beforeLoadNewsViewController
中是否初始化?我知道,descArray
在代码中没有提到。但是由于description
可以是nil
,不要强制将其展开。写入打印(article.articles[I]。description???“n/a”)