我正在使用swift在Xcode 6中开发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/

10-12 12:57
查看更多