简体   繁体   English

NSURLconnection和json

[英]NSURLconnection and json

i know this is a stupid question but i d'ont know how to do that. 我知道这是一个愚蠢的问题,但我不知道该怎么做。 from this link the requested link is possible to return json data with NSURLconnection ? 从此链接请求的链接是否可以使用NSURLconnection返回json数据? I hope some one check this link and tell me if is this possible because i am new in this stuff. 我希望有人检查此链接并告诉我是否可行,因为我是新手。

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. JSON错误:操作无法完成。 (Cocoa error 3840.) (可可错误3840。)

As the comment above suggests, that link doesn't return JSON. 就像上面的注释所暗示的那样,该链接不会返回JSON。 However, assuming you do have such a link, you can use the NSJSONSerialization class to parse JSON data into objective-C classes: 但是,假设您确实有这样的链接,则可以使用NSJSONSerialization类将JSON数据解析为Objective-C类:

http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40010946 http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40010946

Combine this with NSURLConnection and you can do what you ask. 将此与NSURLConnection结合使用,您可以做自己想做的事情。 Here's a walk through on implementing NSURLConnection: 这是实现NSURLConnection的完整过程:

http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html#//apple_ref/doc/uid/20001836-BAJEAIEE http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html#//apple_ref/doc/uid/20001836-BAJEAIEE

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]);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM