Json 我怎样才能解决这个问题?从';[字符串]';不相关类型';[字符串:AnyObject]';总是失败

Json 我怎样才能解决这个问题?从';[字符串]';不相关类型';[字符串:AnyObject]';总是失败,json,swift,foundation,Json,Swift,Foundation,我似乎找不到解决这个问题的办法,有数百个答案,但我找不到与这个简单问题相关的答案。 无论我做什么尝试将字典放入字符串,都失败了? 原文: let jsonResult = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) let jsonString = (jsonResult as AnyObject).components(sep

我似乎找不到解决这个问题的办法,有数百个答案,但我找不到与这个简单问题相关的答案。 无论我做什么尝试将字典放入字符串,都失败了? 原文

let jsonResult = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)
let jsonString = (jsonResult as AnyObject).components(separatedBy: "")                           
let jsonDict = jsonString as! [String: AnyObject]
//let jsonDict = jsonString as! [String: AnyObject]
//let jsonDict = jsonString as! Dictionary<String,String>
//Cast from '[String]' to unrelated type 'Dictionary<String, String>' always fails 
结果是:

This JSON is (most likely) not a string
["Poster": https://m.media-

amazon.com/images/M/MV5BNGNhMDIzZTUtNTBlZi00MTRlLWFjM2ItYzViMjE3YzI5MjljXkEyXkFqcGdeQXVyNzkwMjQ5NzM@._V1_SX300.jpg, "BoxOffice": N/A, "Language": English, Spanish, French, "Year": 1994, "Metascore": 94, "Director": Quentin Tarantino, "Rated": R, "Runtime": 154 min, "Genre": Crime, Drama, "imdbVotes": 1,548,861, "Ratings": <__NSArrayI 0x604000256a10>(
{
    Source = "Internet Movie Database";
    Value = "8.9/10";
},
{
    Source = "Rotten Tomatoes";
    Value = "94%";
},
{
    Source = Metacritic;
    Value = "94/100";
}
)
, "Released": 14 Oct 1994, "imdbRating": 8.9, "Awards": Won 1 Oscar. Another 62 wins & 69 nominations., "Actors": Tim Roth, Amanda Plummer, Laura Lovelace, John Travolta, "Response": True, "Country": USA, "Plot": The lives of two mob hitmen, a boxer, a gangster's wife, and a pair of diner bandits intertwine in four tales of violence and redemption., "DVD": 19 May 1998, "Title": Pulp Fiction, "Writer": Quentin Tarantino (stories), Roger Avary (stories), Quentin Tarantino, "Production": Miramax Films, "imdbID": tt0110912, "Website": N/A, "Type": movie]
此JSON(很可能)不是字符串
[“海报”:https://m.media-
amazon.com/images/M/MV5BNGNhMDIzZTUtNTBlZi00MTRlLWFjM2ItYzViMjE3YzI5MjljXkEyXkFqcGdeQXVyNzkwMjQ5NzM@._V1_SX300.jpg,“票房”:不适用,“语言”:英语、西班牙语、法语,“年份”:1994,“元分数”:94,“导演”:昆汀·塔伦蒂诺,“收视率”:R,“运行时间”:154分钟,“体裁”:犯罪、戏剧,“imdbvoates”:1548861,“收视率”:(
{
Source=“互联网电影数据库”;
Value=“8.9/10”;
},
{
来源=“烂番茄”;
Value=“94%”;
},
{
来源=元批评;
Value=“94/100”;
}
)
“发布”:1994年10月14日,“imdbRating”:8.9,“Awards”:获得1项奥斯卡奖。另外62项奥斯卡奖和69项提名。“演员”:蒂姆·罗斯、阿曼达·普卢默、劳拉·洛夫莱斯、约翰·特拉沃尔塔,“回应”:真实,“国家”:美国,“情节”:两名暴徒杀手、一名拳击手、一名匪徒的妻子和一对食客匪徒的生活交织在四个暴力和救赎的故事中。,“DVD”:1998年5月19日,“标题”:低俗小说,“作家”:昆汀·塔伦蒂诺(故事)、罗杰·艾瓦里(故事)、昆汀·塔伦蒂诺,“制作”:米拉马克斯电影,“imdbID”:tt0110912,“网站”:不适用,“类型”:电影]

首先绝不使用
AnyObject
获取Swift 3+中的JSON值。它们都是
Any

发生此错误的原因是
组件(由
分隔)的结果是一个数组(
[String]
),但您将其强制转换为字典(
[String:Any(Object)]
)。完全不要强制转换,编译器知道类型

并且不要在Swift中使用
.mutableContainers
,永远不要。此选项毫无意义。

组件(由
分隔)只有在JSON是字符串时才有意义。如果是这样,您必须传递选项
。allowFragments

编辑:

This JSON is (most likely) not a string
["Poster": https://m.media-

amazon.com/images/M/MV5BNGNhMDIzZTUtNTBlZi00MTRlLWFjM2ItYzViMjE3YzI5MjljXkEyXkFqcGdeQXVyNzkwMjQ5NzM@._V1_SX300.jpg, "BoxOffice": N/A, "Language": English, Spanish, French, "Year": 1994, "Metascore": 94, "Director": Quentin Tarantino, "Rated": R, "Runtime": 154 min, "Genre": Crime, Drama, "imdbVotes": 1,548,861, "Ratings": <__NSArrayI 0x604000256a10>(
{
    Source = "Internet Movie Database";
    Value = "8.9/10";
},
{
    Source = "Rotten Tomatoes";
    Value = "94%";
},
{
    Source = Metacritic;
    Value = "94/100";
}
)
, "Released": 14 Oct 1994, "imdbRating": 8.9, "Awards": Won 1 Oscar. Another 62 wins & 69 nominations., "Actors": Tim Roth, Amanda Plummer, Laura Lovelace, John Travolta, "Response": True, "Country": USA, "Plot": The lives of two mob hitmen, a boxer, a gangster's wife, and a pair of diner bandits intertwine in four tales of violence and redemption., "DVD": 19 May 1998, "Title": Pulp Fiction, "Writer": Quentin Tarantino (stories), Roger Avary (stories), Quentin Tarantino, "Production": Miramax Films, "imdbID": tt0110912, "Website": N/A, "Type": movie]
根据您添加的结果,收到的对象显然是一个字典,因此
as?String
以及
组件(以“
分隔)。allowFragments
是错误的。请尝试此操作

if let jsonDictionary = try JSONSerialization.jsonObject(with: data!) as? [String:Any] {
   for (key, value) in jsonDictionary {
       print(key, value)
   }
} else {
    print("This JSON is not a dictionary")
} 

为什么我们不应该永远在Swift中使用.mutableContainers?你能举一个例子吗?Adobels这个选项适用于Objective-C中的
NSMutable…
集合类型。它在Swift中没有任何效果,在Swift中,你可以通过
var
免费获得可变性。除此之外,几乎所有的教程建议
.mutableContainers
将值赋给一个不可变常量(如问题中所述),这是两次胡说八道。@瓦迪安,至少现在可以正常工作了。不过,我得到的是“这个JSON(很可能)不是字符串”以及打印出来的数据(jsonDict)。我必须编辑我的原始帖子才能全部显示。我可以这样做吗?我知道结果不能是字符串。我更新了答案。请不要编辑添加建议解决方案的问题。这会让其他有相同或类似问题的读者感到困惑。
if let jsonDictionary = try JSONSerialization.jsonObject(with: data!) as? [String:Any] {
   for (key, value) in jsonDictionary {
       print(key, value)
   }
} else {
    print("This JSON is not a dictionary")
}