gpt4 book ai didi

json - 在 Swift 中解析 JSON 不起作用

转载 作者:行者123 更新时间:2023-11-30 10:19:55 24 4
gpt4 key购买 nike

我正在 Xcode 6 中使用 swift 开发 IOS 应用程序。我是这门语言的新手,老实说,我正在拔掉我仅有的一点头发。

使用 b NSLog("%@", allResults) 时,我将在对 LastFM 的 API 调用中看到以下 JSON 结果

 artist =     {
bio = {
content = "\n Cher (born Cherilyn Sarkisian; May 20, 1946) is an Oscar - and Grammy- winning American singer and actress. A major figure for over five decades in the world of popular culture, she is often referred to as the Goddess of Pop for having first brought the sense of female autonomy and self-actualization into the entertainment industry. She is known for her distinctive contralto and for having worked extensively across media, as well as for continuously reinventing both her music and image, the latter of which has been known to induce controversy. \n\n <a href=\"http://www.last.fm/music/Cher\">Read more about Cher on Last.fm</a>.\n \n \nUser-contributed text is available under the Creative Commons By-SA License and may also be available under the GNU FDL.\n ";
formationlist = {
formation = {
yearfrom = 1965;
yearto = "";
};
};
links = {
link = {
"#text" = "";
href = "http://www.last.fm/music/Cher/+wiki";
rel = original;
};
};
published = "Thu, 6 Mar 2014 11:40:46 +0000";
summary = "\n Cher (born Cherilyn Sarkisian; May 20, 1946) is an Oscar - and Grammy- winning American singer and actress. A major figure for over five decades in the world of popular culture, she is often referred to as the Goddess of Pop for having first brought the sense of female autonomy and self-actualization into the entertainment industry. She is known for her distinctive contralto and for having worked extensively across media, as well as for continuously reinventing both her music and image, the latter of which has been known to induce controversy. \n\n <a href=\"http://www.last.fm/music/Cher\">Read more about Cher on Last.fm</a>.\n \n ";
yearformed = 1965;
};
image = (
{
"#text" = "http://userserve-ak.last.fm/serve/34/63186903.png";
size = small;
},
{
"#text" = "http://userserve-ak.last.fm/serve/64/63186903.png";
size = medium;
},
{
"#text" = "http://userserve-ak.last.fm/serve/126/63186903.png";
size = large;
},
{
"#text" = "http://userserve-ak.last.fm/serve/252/63186903.png";
size = extralarge;
},
{
"#text" = "http://userserve-ak.last.fm/serve/500/63186903/Cher.png";
size = mega;
}
);
mbid = "bfcc6d75-a6a5-4bc6-8282-47aec8531818";
name = Cher;
ontour = 1;
similar = {
artist = (
{
image = (
{
"#text" = "http://userserve-ak.last.fm/serve/34/98934631.png";
size = small;
},
{
"#text" = "http://userserve-ak.last.fm/serve/64/98934631.png";
size = medium;
},
{
"#text" = "http://userserve-ak.last.fm/serve/126/98934631.png";
size = large;
},
{
"#text" = "http://userserve-ak.last.fm/serve/252/98934631.png";
size = extralarge;
},
{
"#text" = "http://userserve-ak.last.fm/serve/_/98934631/Madonna+M.png";
size = mega;
}
);
name = Madonna;
url = "http://www.last.fm/music/Madonna";
},
{
image = (
{
"#text" = "http://userserve-ak.last.fm/serve/34/32340147.png";
size = small;
},
{
"#text" = "http://userserve-ak.last.fm/serve/64/32340147.png";
size = medium;
},
{
"#text" = "http://userserve-ak.last.fm/serve/126/32340147.png";
size = large;
},
{
"#text" = "http://userserve-ak.last.fm/serve/252/32340147.png";
size = extralarge;
},
{
"#text" = "http://userserve-ak.last.fm/serve/500/32340147/Sonny++Cher+sonnycherbw2.png";
size = mega;
}
);
name = "Sonny & Cher";
url = "http://www.last.fm/music/Sonny+&+Cher";
},
{
image = (
{
"#text" = "http://userserve-ak.last.fm/serve/34/98367071.png";
size = small;
},
{
"#text" = "http://userserve-ak.last.fm/serve/64/98367071.png";
size = medium;
},
{
"#text" = "http://userserve-ak.last.fm/serve/126/98367071.png";
size = large;
},
{
"#text" = "http://userserve-ak.last.fm/serve/252/98367071.png";
size = extralarge;
},
{
"#text" = "http://userserve-ak.last.fm/serve/500/98367071/Kylie+Minogue+VOGUE+AU+PNG+HQ.png";
size = mega;
}
);
name = "Kylie Minogue";
url = "http://www.last.fm/music/Kylie+Minogue";
},
{
image = (
{
"#text" = "http://userserve-ak.last.fm/serve/34/96074665.png";
size = small;
},
{
"#text" = "http://userserve-ak.last.fm/serve/64/96074665.png";
size = medium;
},
{
"#text" = "http://userserve-ak.last.fm/serve/126/96074665.png";
size = large;
},
{
"#text" = "http://userserve-ak.last.fm/serve/252/96074665.png";
size = extralarge;
},
{
"#text" = "http://userserve-ak.last.fm/serve/_/96074665/Cline+Dion.png";
size = mega;
}
);
name = "C\U00e9line Dion";
url = "http://www.last.fm/music/C%C3%A9line+Dion";
},
{
image = (
{
"#text" = "http://userserve-ak.last.fm/serve/34/97259211.png";
size = small;
},
{
"#text" = "http://userserve-ak.last.fm/serve/64/97259211.png";
size = medium;
},
{
"#text" = "http://userserve-ak.last.fm/serve/126/97259211.png";
size = large;
},
{
"#text" = "http://userserve-ak.last.fm/serve/252/97259211.png";
size = extralarge;
},
{
"#text" = "http://userserve-ak.last.fm/serve/500/97259211/RuPaul+PNG.png";
size = mega;
}
);
name = RuPaul;
url = "http://www.last.fm/music/RuPaul";
}
);
};
stats = {
listeners = 940507;
playcount = 11967702;
};
streamable = 0;
tags = {
tag = (
{
name = pop;
url = "http://www.last.fm/tag/pop";
},
{
name = "female vocalists";
url = "http://www.last.fm/tag/female%20vocalists";
},
{
name = 80s;
url = "http://www.last.fm/tag/80s";
},
{
name = dance;
url = "http://www.last.fm/tag/dance";
},
{
name = rock;
url = "http://www.last.fm/tag/rock";
}
);
};
url = "http://www.last.fm/music/Cher";
};

我尝试了以下方法:

func didRecieveAPIRequest(results, NSDictionary) {
let results: NSArray = results["artist'] as NSArray

我抛出 EXC_BREAKPOINT 并发生崩溃。所以尝试了字典路线,我可以看到数据。事实上,我可以获取名称和简介,但无法获取特定的图像 URL。我认为这与嵌套有关,但此时我不知所措,因为它与对 Itunes API 的调用一起工作。这可能是一个菜鸟错误,但我们将不胜感激。

如果需要更多信息,请告诉我,请仅提供建设性的答复。

提前致谢。

最佳答案

我认为您将结果转换为错误的类型。 artist 键保存的是字典,而不是数组:

试试这个:

func didRecieveAPIRequest(results: NSDictionary) {
if let results = results["artist"] as? NSDictionary {
println("Dictionary")
} else {
println("Something Else")
}
}

关于json - 在 Swift 中解析 JSON 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27458610/

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