在Swift中解析JSON无法正常工作

时间:2014-12-13 12:10:13

标签: json swift last.fm

我正在使用swift在Xcode 6中开发一个IOS应用程序。我是这门语言的新手,说实话,我正在拔出我的小头发。

当使用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。它可能是一个新手的错误,但是非常感谢帮助。

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

提前致谢。

1 个答案:

答案 0 :(得分:1)

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

试试这个:

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