Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将JSON解析为UILabel时出现空数组错误_Ios_Objective C_Json_Parsing - Fatal编程技术网

Ios 将JSON解析为UILabel时出现空数组错误

Ios 将JSON解析为UILabel时出现空数组错误,ios,objective-c,json,parsing,Ios,Objective C,Json,Parsing,我正在尝试解析服务器上JSON文件中的一些文本。我正在解析文件中的三个不同值:“value1”:“some\u value”,“value2”:“some\u value”,“value3”:“some\u value” 我正在声明一个名为stathome的NSMutableArray,我在viewDidLoad方法中初始化它,如下所示:stathome=[[NSMutableArray alloc]init] 问题是,在初始化数组之后,我需要设置一个NSDictionary。如果我在表视图中这

我正在尝试解析服务器上JSON文件中的一些文本。我正在解析文件中的三个不同值:
“value1”:“some\u value”
“value2”:“some\u value”
“value3”:“some\u value”

我正在声明一个名为
stathome
的NSMutableArray,我在viewDidLoad方法中初始化它,如下所示:
stathome=[[NSMutableArray alloc]init]

问题是,在初始化数组之后,我需要设置一个NSDictionary。如果我在表视图中这样做,我会写:

NSDictionary *stats = [self.statshome objectAtIndex:indexPath.row];
但我没有将这些值解析为表视图。我想把它们解析成UILabel。所以我尝试的是声明新的
NSUInteger
。因为我不能在viewDidLoad方法中使用
indexPath.row
。我称之为
statindex

但当我启动应用程序时,它崩溃并向我显示以下错误消息:
由于未捕获的异常“NSRangeException”而终止应用程序,原因:“***-[\uu NSArrayM objectAtIndex:]:索引0超出空数组的界限”

代码的其余部分如下所示:

@interface DEMOHomeViewController () {
    NSUInteger statsIndex;
}
@end

@implementation DEMOHomeViewController
@synthesize statsHome;
@synthesize twitterFollowers;
@synthesize facebookLikes;
@synthesize instagramFollowers;

- (void)viewDidLoad
{
    [super viewDidLoad];  

    statsHome = [[NSMutableArray alloc] init];

    NSDictionary *stats = [self.statsHome objectAtIndex:statsIndex];

    value1.text = [stats objectForKey:@"value1"];
    value2.text = [stats objectForKey:@"value2"];
    value3.text = [stats objectForKey:@"value3"];

    NSURL *url1 = [[NSURL alloc] initWithString:@"the-json-file-with-the-values.php/file.json"];
    NSURLRequest *request1 = [[NSURLRequest alloc] initWithURL:url1];

    AFJSONRequestOperation *operation1 = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        self.statsHome = [[NSArray alloc] initWithArray:JSON];
        [self.activityIndicatorView stopAnimating];

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
    }];

    [operation1 start];

}

@end

错误是正确的:您试图在设置数据之前从数组中获取数据。您应该在下面的代码中执行类似的操作

- (void)viewDidLoad
{
    [super viewDidLoad];  

    NSURL *url1 = [[NSURL alloc] initWithString:@"the-json-file-with-the-values.php/file.json"];
    NSURLRequest *request1 = [[NSURLRequest alloc] initWithURL:url1];

    AFJSONRequestOperation *operation1 = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        [self.activityIndicatorView stopAnimating];
        [self updateDataWithJSON:JSON]; // <- Here we will update our data
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
    }];

    [operation1 start];

}

- (void)updateDataWithJSON:(id)JSON 
{
    self.statsHome = [[NSArray alloc] initWithArray:JSON];

    NSDictionary *stats = [self.statsHome objectAtIndex:statsIndex];

    value1.text = [stats objectForKey:@"value1"];
    value2.text = [stats objectForKey:@"value2"];
    value3.text = [stats objectForKey:@"value3"];
}
-(void)viewDidLoad
{
[超级视图下载];
NSURL*url1=[[NSURL alloc]initWithString:@“值为.php/file.json的json文件”;
NSURLRequest*request1=[[NSURLRequest alloc]initWithURL:url1];
AFJSONRequestOperation*operation1=[AFJSONRequestOperation JSONRequestOperationWithRequest:请求成功:^(NSURLRequest*请求,NSHTTPURLRResponse*响应,id JSON){
[self.activityIndicatorView停止设置动画];

[self-updateDataWithJSON:JSON];//NSURL*url=[NSURL URLWithString:[NSString STRING stringWithFormat:@“url”]


在第二行中,您的alloc/init
stathome
(与
self.stathome
?)为空,您直接尝试从中获取一个值(
objectAtIndex:statsIndex
),因此,它崩溃是正常的。那么我应该怎么做呢?@LarmeI将等待,直到我得到我的值(通过JSON请求),一旦donc设置了标签。我该怎么做?@LarmeCool。但是现在,当我启动应用程序时,标签变为空。似乎什么都没有被解析…@anatoliy_v@tracifycray您可以隐藏标签,直到收到数据,然后在updateDataWithJSON:method中取消隐藏。这是请求/响应的常见做法……但我已经尝试等待了8分钟以上tes,标签上没有显示任何内容……有点不对劲@anatoliy_vHere有几件事你可以检查以得到答案:1)你得到了响应吗?2)响应中的JSON是什么?3)updateDataWithJSON:调用了吗?4)你添加了value1.hidden=NO到updateDataWithJSON:?嗯,搞不清楚。你有邮件地址吗?如果你在日志中得到了数据。然后你做你的部分。所有这些代码都应该在viewDidLoad方法中吗?@VMC501是的,你可以在view did load方法中使用loading json url。。或者你可以使用view will Display方法。[请求setHTTPMethod:@“GET”];不要忘记设置Http方法。如果你不设置任何方法,它将返回空的json响应
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"GET"];

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

NSString *authStr = [NSString stringWithFormat:@"%@:%@", @"usernmae", @"password"];

NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];

NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedStringWithOptions:0]];

NSLog(@"%@",authValue);//

[request setValue:authValue forHTTPHeaderField:@"Authorization"];

[request setURL:url];
NSLog(@"%@",url);

NSError *error;

NSHTTPURLResponse *response;

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSLog(@"Response code: %ld", (long)[response statusCode]);

NSLog(@"Data is %@",data);