gpt4 book ai didi

iphone - 检查 last.fm 返回的 json 中是否存在 key

转载 作者:行者123 更新时间:2023-11-29 13:17:29 26 4
gpt4 key购买 nike

我正在编写一个 iOS 应用程序,它使用 last.fm 网络服务来搜索专辑。以下代码将返回的 json 转换为 NSArray:

results = (NSArray *)[[[json valueForKey:@"results"] valueForKey:@"albummatches"] valueForKey:@"album"];

这在搜索词返回结果时工作正常,但在没有返回结果时应用程序崩溃。这是因为找不到结果时,json 中不存在键“album”。

如何让我的应用程序检查 json 中是否存在“专辑”键?

编辑:没有结果时返回示例 json。搜索字符串是“asdfgasdfg”。请注意 albumbatches 键下的字符返回。

2013-03-12 14:08:00.889 MyMusicLibrary[387:1a03] {
results = {
"@attr" = {
for = asdfgasdfg;
};
albummatches = "\n";
"opensearch:Query" = {
"#text" = "";
role = request;
searchTerms = asdfgasdfg;
startPage = 1;
};
"opensearch:itemsPerPage" = 50;
"opensearch:startIndex" = 0;
"opensearch:totalResults" = 0;
};
}

编辑 2:返回结果时的示例 json:

2013-03-12 14:19:42.769 MyMusicLibrary[880:1a03] {
results = {
"@attr" = {
for = coldplay;
};
albummatches = {
album = (
{
artist = Coldplay;
id = 1416655;
image = (
{
"#text" = "http://userserve-ak.last.fm/serve/34s/85845017.png";
size = small;
},
{
"#text" = "http://userserve-ak.last.fm/serve/64s/85845017.png";
size = medium;
},
{
"#text" = "http://userserve-ak.last.fm/serve/126/85845017.png";
size = large;
},
{
"#text" = "http://userserve-ak.last.fm/serve/300x300/85845017.png";
size = extralarge;
}
);
mbid = "c67be398-9417-4c22-bc13-d6158029ae21";
name = "A Rush of Blood to the Head";
streamable = 1;
url = "http://www.last.fm/music/Coldplay/A+Rush+of+Blood+to+the+Head";
},
{
artist = Coldplay;
id = 1984;
image = (
{
"#text" = "http://userserve-ak.last.fm/serve/34s/87203265.png";
size = small;
},
{
"#text" = "http://userserve-ak.last.fm/serve/64s/87203265.png";
size = medium;
},
{
"#text" = "http://userserve-ak.last.fm/serve/126/87203265.png";
size = large;
},
{
"#text" = "http://userserve-ak.last.fm/serve/300x300/87203265.png";
size = extralarge;
}
);
mbid = "8e602038-c0f2-3c2d-9068-a1a3daca493d";
name = Parachutes;
streamable = 1;
url = "http://www.last.fm/music/Coldplay/Parachutes";
}
);
};
"opensearch:Query" = {
"#text" = "";
role = request;
searchTerms = coldplay;
startPage = 1;
};
"opensearch:itemsPerPage" = 2;
"opensearch:startIndex" = 0;
"opensearch:totalResults" = 375;
};
}

最佳答案

有两种方法可以解决这个问题

  1. 假设 "opensearch:totalResults" 实际上是正确的结果计数,那么您可以使用此值来确定是否有结果,然后使用

    NSArray *results = nil;

    if ([[json valueForKeyPath:@"results.opensearch:totalResults"] intValue] > 0) {
    results = [json valueForKeyPath:@"results.albummatches.album"];
    }
  2. 在处理之前检查你有一个字符串还是一个数组

    id albummatches = [json valueForKeyPath:@"results.albummatches"];

    // is this a dictionary or a string?
    // You can either ask if it implements the interface or ask what class it is. I prefer the first
    BOOL isDictionary = [albummatches respondsToSelector:@selector(objectForKey:)];

    // or
    BOOL isDictionary = [albummatches isKindOfClass:[NSDictionary class]];

    if (isDictionary) {
    results = [albummatches objectForKey:@"album"];
    }

关于iphone - 检查 last.fm 返回的 json 中是否存在 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15343183/

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