如何在Objective-C中解析JSONP? [英] How to parse JSONP in Objective-C?
问题描述
我正在检索API的JSON信息,它在API上说它是JSON但我注意到它是在JSONP或json with padding中有人称之为。我厌倦了到处寻找如何解析这个但没有运气。我试图收到的信息是这样的:
I am retrieving JSON information for an API and it says on the API that it is in JSON but I noticed it is in JSONP or "json with padding" as some call it. I tired to look everywhere to find how to parse this but no luck. The information I am trying to receive is this:
({"book":[{"book_name":"James","book_nr":"59","chapter_nr":"3","chapter":
{"16":{"verse_nr":"16","verse":"For where envying and strife is, there is confusion and
every evil work."}}}],"direction":"LTR","type":"verse"});
数据的链接是 https://getbible.net/json?p = James3:16 ,所以你可以直接查看它。
The link to the data is https://getbible.net/json?p=James3:16, so you can look at it directly.
这是我用来尝试检索JSON数据的代码并将其解析为NSMutableDictionary。
This is the code I am using to try to retrieve the JSON Data and parse it into a NSMutableDictionary.
-(void)fetchJson {
NSString *currentURL = [NSString stringWithFormat:@"https://getbible.net/json?p=James"];
NSURL *url = [NSURL URLWithString:currentURL];
NSData *data = [[NSData alloc]initWithContentsOfURL:url];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
NSMutableData *receivedData = [[NSMutableData alloc] initWithLength:0];
NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
[receivedData setLength:0];
NSURLResponse *response = [[NSURLResponse alloc] initWithURL:url MIMEType:@".json" expectedContentLength:-1 textEncodingName:nil];
expectedTotalSize = [response expectedContentLength];
if ([data length] !=0) {
NSLog(@"appendingData");
[receivedData appendData:data];
if(connection){
NSLog(@"Succeeded! Received %lu bytes of data",(unsigned long)[receivedData length]);
}
NSError *error;
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
if(jsonResponse){
NSArray *responseArr = [jsonResponse mutableCopy];
NSLog(@"%lu",(unsigned long)[responseArr count]);
}else if (!jsonResponse){
//do internet connection error response
}
}
}
我在代码中设置断点的结果是:
The results I am getting back from putting a breakpoint in the code is:
- jsonResponse返回NULL
- NSError NSCocoaErrorDomain代码 - 3840
- 但我的NSData *数据返回15640字节。
- jsonResponse returns NULL
- NSError NSCocoaErrorDomain code - 3840
- but my NSData *data is returning 15640 bytes.
我的控制台是从用于调试的NSLogs中显示的:
My console is displaying this from the NSLogs I used for debugging:
2014-04-20 01:27:31.877 appendingData
2014-04-20 01:27:31.879 Succeeded! Received 15640 bytes of data
我正确地收到数据,但我没有正确解析它我知道错误是因为JSON采用JSONP格式。如果有人能帮忙解决这个问题,我会非常感激。我已经厌倦了尽可能详细地提供这个问题,但是如果您需要更多信息,请告诉我,以便我可以添加它并使其尽可能清晰。
I am receiving the data correctly but I am not parsing it correctly I know the error is because the JSON is in JSONP format. If anyone could please help with this I would appreciate it so much. I have tired to give as much detail on this question as I can but if you need more information just let me know so I can add it and make this as clear as possible.
推荐答案
您的代码至少有两次单独的下载数据尝试。两者都不是真的正确。该代码也只适用于JSON,而不适用于JSONP。
Your code has at least two separate attempts to download the data. Neither is really correct. The code also only works with JSON, not JSONP.
试试这个:
NSURL *url = [NSURL URLWithString:@"https://getbible.net/json?p=James"];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (data) {
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSRange range = [jsonString rangeOfString:@"("];
range.location++;
range.length = [jsonString length] - range.location - 2; // removes parens and trailing semicolon
jsonString = [jsonString substringWithRange:range];
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *jsonError = nil;
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&jsonError];
if (jsonResponse) {
// process jsonResponse as needed
} else {
NSLog(@"Unable to parse JSON data: %@", jsonError);
}
} else {
NSLog(@"Error loading data: %@", error);
}
}];
这篇关于如何在Objective-C中解析JSONP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!