从JSON中提取信息

时间:2013-12-01 19:25:16

标签: ios objective-c json

我想得到平均温度。我对识别格式不是很好,但我很确定结果是解析json。要了解我如何使用结果以及我已经厌倦了这里做的是我正在使用的代码以及openWeatherMapAPI iOS包装器链接:

https://github.com/adba/OpenWeatherMapAPI

我运行此方法:

[weatherAPI dailyForecastWeatherByCityName:destination 
                                 withCount:time 
                               andCallback:^(NSError *error, NSDictionary *result) { 

结果如下:

 clouds = 32;
    deg = 123;
    dt = "2013-12-02 11:00:00 +0000";
    humidity = 86;
    pressure = "1039.92";
    speed = "1.41";
    temp =     {
        day = "8.429986572265648";
        eve = "6.420007324218773";
        max = "8.429986572265648";
        min = "1.339990234375023";
        morn = "2.189996337890648";
        night = "1.339990234375023";
    };
    weather =     (
                {
            description = "nuages \U00e9parses";
            icon = 03d;
            id = 802;
            main = Clouds;
        }
    );
}

·H:

OWMWeatherAPI *weatherAPI;

NSArray *forecast;
NSDateFormatter *dateFormatter;

的.m:

    [weatherAPI dailyForecastWeatherByCityName:destination withCount:time andCallback:^(NSError *error, NSDictionary *result) {
        if (error) {

            [activityIndicator stopAnimating];
            activityIndicator.hidden = YES;

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please verify you have an internet connection and that the city name is spelled correclty with no spaces afterwars or before it." delegate:Nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];

            [alert show];
        }

        // The data is ready

        forecast = result[@"list"];

        [self.forecastTableView reloadData];
    }];
}

#pragma mark - forecast tableview datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return forecast.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }

    NSDictionary *forecastData = [forecast objectAtIndex:indexPath.row];

    cell.textLabel.text = [NSString stringWithFormat:@"%.1f℃ - %@", [forecastData[@"main"][@"temp"] floatValue], forecastData[@"weather"][0][@"main"]
                           ];

    cell.detailTextLabel.text = [dateFormatter stringFromDate:forecastData[@"dt"]];

    return cell;
}

1 个答案:

答案 0 :(得分:0)

经过大量错误和试用后,我发现如何获取该文件中任何键的任何值。 错误列在这一行:

    cell.textLabel.text = [NSString stringWithFormat:@"%.1f℃ - %@", [forecastData[@"main"][@"temp"] floatValue], forecastData[@"weather"][0][@"main"]

要获得temp中的日期值,您需要执行以下操作:

    cell.textLabel.text = [NSString stringWithFormat:@"%.1f℃ - %@", [forecastData[@"day"][@"temp"] floatValue], forecastData[@"weather"][0][@"main"]

或者说湿度:

  .....[@"humidity"].....