los*_*hua 1 parsing jsonp objective-c ios
我正在检索API的JSON信息,它在API上说它是JSON但我注意到它是在JSONP或"带有填充的json"中,有些人称之为.我厌倦了到处寻找如何解析这个,但没有运气.我想收到的信息是这样的:
({"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"});
Run Code Online (Sandbox Code Playgroud)
数据的链接是 https://getbible.net/json?p=James3:16,所以你可以直接看一下.
这是我用来尝试检索JSON数据并将其解析为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
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在代码中设置断点的结果是:
我的控制台是从我用于调试的NSLog中显示的:
2014-04-20 01:27:31.877 appendingData
2014-04-20 01:27:31.879 Succeeded! Received 15640 bytes of data
Run Code Online (Sandbox Code Playgroud)
我正确地接收数据但我没有正确解析它我知道错误是因为JSON是JSONP格式.如果有人可以请求帮助,我会非常感激.我已经厌倦了尽可能详细地提供这个问题,但是如果您需要更多信息,请告诉我,以便我可以添加它并使其尽可能清晰.
您的代码至少有两次单独的下载数据尝试.两者都不是真的正确.该代码也只适用于JSON,而不适用于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);
}
}];
Run Code Online (Sandbox Code Playgroud)