xcode 来自 JSON 文件的 iOS 6 NSDictionary

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12829289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:46:28  来源:igfitidea点击:

iOS 6 NSDictionary from JSON File

iosxcodejsonios6nsdictionary

提问by gtr123

I am building a project using story boards and a MasterViewlayout. I'm trying to get the UITableViewdata from a JSON file (http://afterimagedesign.tk/topHits.jsonI don't know if this is correct JSON or not, I'm new) so I call these methods in viewDidLoad()

我正在使用故事板和MasterView布局构建一个项目。我正在尝试UITableView从 JSON 文件中获取数据(http://afterimagedesign.tk/topHits.json我不知道这是否是正确的 JSON,我是新手)所以我将这些方法称为viewDidLoad()

responseData = [[NSMutableData data] retain];
NSURLRequest *topHitsRequest = [NSURLRequest requestWithURL:[NSURLURLWithString:@"http://afterimagedesign.tk/topHits.json"]];
[[NSURLConnection alloc] initWithRequest:topHitsRequest delegate:self];

and then in connectionDidFinishLoading:(NSURLConnection *)connectionmethod, I call this:

然后在connectionDidFinishLoading:(NSURLConnection *)connection方法中,我称之为:

[connection release]
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSDictionary *topHitsDictionary = [responseString JSONValue];
NSArray *topHitsArray = [topHitsDictionary valueForKey:@"title"];

The problem is (I think) that I get this error: "Instance method '-JSONValue' not found." I have looked for this for a while, and all of the tutorials I've seen use this method. Is it iOS 6 or something else? Thanks!

问题是(我认为)我收到此错误:“未找到实例方法‘-JSONValue’。” 我已经找了一段时间了,我看到的所有教程都使用这种方法。是 iOS 6 还是别的什么?谢谢!

EDIT:

编辑:

How do I add it to a UITableView? I tried cell.textLabel.text = [self.topKpop objectAtIndex:indexPath.row];but I keep getting a SIGABRT error. I have gotten SIGABRT error, but this time it's in the .m file.

如何将它添加到 UITableView?我试过了,cell.textLabel.text = [self.topKpop objectAtIndex:indexPath.row];但我一直收到 SIGABRT 错误。我收到了 SIGABRT 错误,但这次是在 .m 文件中。

回答by Andrew

I think the tutorials were using SBJsonor similar JSON parser frameworks.

我认为教程正在使用SBJson或类似的 JSON 解析器框架。

If you don't want to use any external frameworks, you can try JSONObjectWithData:options:error:from the NSJSONSerializationclass, which is available since iOS 5.0.

如果您不想使用任何外部框架,您可以JSONObjectWithData:options:error:NSJSONSerialization类中尝试,该类自 iOS 5.0 起可用。

[connection release]
NSError *jsonError = nil;
NSDictionary *topHitsDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonError];
NSArray *topHitsArray = [topHitsDictionary valueForKey:@"title"];
// ...
[responseData release];