无法读取通过ios7中的url下载和保存的某些json文件
我在阅读一些.json文件时遇到了问题,它在可以通过IE像网页一样读取的json上运行良好,但在url像ftp样式一样尝试下载的文件上不起作用 最后,我正在考虑一旦成功就用json信息填充标签,但看不出是什么导致了URLRequest 这就是我测试它的代码:无法读取通过ios7中的url下载和保存的某些json文件,json,ios7,xcode5,Json,Ios7,Xcode5,我在阅读一些.json文件时遇到了问题,它在可以通过IE像网页一样读取的json上运行良好,但在url像ftp样式一样尝试下载的文件上不起作用 最后,我正在考虑一旦成功就用json信息填充标签,但看不出是什么导致了URLRequest 这就是我测试它的代码: // WDViewController.h #import <UIKit/UIKit.h> @interface WDViewController : UIViewController{ NSArray *jsonA
// WDViewController.h
#import <UIKit/UIKit.h>
@interface WDViewController : UIViewController{
NSArray *jsonArray;
NSMutableData *data;
}
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;
-(IBAction)WDPlayerButtonUpdate:(id)sender;
@end
// WDViewController.m
#import "WDViewController.h"
@interface WDViewController ()
@end
@implementation WDViewController
@synthesize outputLabel;
-(IBAction)WDPlayerButtonUpdate:(id)sender{
NSURL *url = [NSURL URLWithString:@"http://gooruism.com/feed/json"];
NSURLRequest *data = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
data = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData{
[data appendData:theData];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
jsonArray = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
// Loop through jsonArray
for (int i = 0; i< jsonArray.count; i++)
{
outputLabel.text = [[jsonArray objectAtIndex:i] objectForKey:@"889"]; // Set Labels
}
}
@end
json文件和url只是我在Google上寻找解决方案时发现的,仅供测试使用。在更多的Google和对json的了解之后, 我让代码正常工作了 结果是空索引的空豁免,并且没有正确调用objectForKeys,因为在使用填充标签时留下了出错的字符串 代码我终于开始工作了:
//
// WDViewController.m
// JSON Test
#import "WDViewController.h"
@interface WDViewController ()
@end
@implementation WDViewController
@synthesize testLabel;
-(IBAction)WDPlayerButtonUpdate:(id)sender
{
// Set network activity indicator
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
// Set json url.
NSURL *url = [NSURL URLWithString:@"<url for json>"];
// get the url page into the nsdata object.
NSData *jsonData = [NSData dataWithContentsOfURL:url];
// Read json (from jasondata) and convert to object.
if ( jsonData != nil)
{
NSError *error = nil;
id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
if (error == nil)
NSLog (@"%@", result);
NSLog(@"Test 1");
// Retrieve array and show in log
NSLog(@"From status index: %@", [result objectForKey:@"status"]);
// Retrieve data and log
NSLog(@"From profile.handle index: %@", [[result objectForKey:@"profile"]objectForKey:@"handle"]);
NSLog(@"Next Test 2"); // Populate label with data handle
yestLabel.text = [[result objectForKey:@"profile"]objectForKey:@"handle"];
NSLog(@"End of Tests");
}
}
@end