xcode NSURLconnection 和 json
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11319572/
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
NSURLconnection and json
提问by adellam
i know this is a stupid question but i d'ont know how to do that. from this link the requested linkis possible to return json data with NSURLconnection ? I hope some one check this link and tell me if is this possible because i am new in this stuff.
我知道这是一个愚蠢的问题,但我不知道该怎么做。从此链接 请求的链接可以使用 NSURLconnection 返回 json 数据?我希望有人检查这个链接并告诉我这是否可能,因为我是这方面的新手。
EDIT:
编辑:
i tried with NSJSONSerialization
我尝试使用NSJSONSerialization
- (void)viewDidLoad
{
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.goalzz.com/main.aspx?region=-1&area=6&update=true"]];
connectionData = [[NSURLConnection alloc]initWithRequest:req delegate:self];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
Data = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[Data appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSError *jsonParsingError = nil;
id object = [NSJSONSerialization JSONObjectWithData:Data options:0 error:&jsonParsingError];
if (jsonParsingError) {
NSLog(@"JSON ERROR: %@", [jsonParsingError localizedDescription]);
} else {
NSLog(@"OBJECT: %@", [object class]);
}
}
and I get this error message in the console :
我在控制台中收到此错误消息:
JSON ERROR:The operation couldn't be completed. (Cocoa error 3840.)
JSON 错误:操作无法完成。(可可错误 3840。)
回答by ChrisH
As the comment above suggests, that link doesn't return JSON. However, assuming you do have such a link, you can use the NSJSONSerialization class to parse JSON data into objective-C classes:
正如上面的评论所暗示的那样,该链接不返回 JSON。但是,假设您确实有这样的链接,您可以使用 NSJSONSerialization 类将 JSON 数据解析为 Objective-C 类:
Combine this with NSURLConnection and you can do what you ask. Here's a walk through on implementing NSURLConnection:
将此与 NSURLConnection 结合使用,您就可以按照您的要求进行操作。这是实现 NSURLConnection 的演练:
And here's an outline of what you'd need. Obviously this is not working code:
这是您需要的概述。显然这不是工作代码:
- (void)downloadJSONFromURL {
NSURLRequest *request = ....
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
// ...
}
NSMutableData *urlData;
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
urlData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[urlData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSError *jsonParsingError = nil;
id object = [NSJSONSerialization JSONObjectWithData:urlData options:0 error:&jsonParsingError];
if (jsonParsingError) {
DLog(@"JSON ERROR: %@", [jsonParsingError localizedDescription]);
} else {
DLog(@"OBJECT: %@", [object class]);
}
}