从URL检索和显示JSON数据(objective-C)

时间:2012-11-11 04:12:27

标签: iphone objective-c json asihttprequest

我正在做功课,使用单例模式和ASIHTTPRequest从JSON对象中检索和显示有关当前天气的信息。

来自JSON格式的URL的数据如下所示:

  { "data": 
        { "current_condition": 
            [ {
             "cloudcover": "51", 
             "humidity": "66", 
             "observation_time": "12:44 PM", 
             "precipMM": "0.0", 
             "pressure": "1002", 
             "temp_C": "30", 
             "temp_F": "86", 


        "visibility": "10", 
         "weatherCode": "116",  
         "weatherDesc": [ {"value": "Partly Cloudy" } ], 
         "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0004_black_low_cloud.png" } ], 
         "winddir16Point": "S", 
         "winddirDegree": "170", 
         "windspeedKmph": "19", 
         "windspeedMiles": "12" } ],  
        "request": [ {
                    "query": "Lat 22.49 and Lon 114.14", 
                    "type": "LatLon" } ],  
        "weather": [ {
                    "date": "2012-06-06", 
                    "precipMM": "0.0", 
                    "tempMaxC": "30", 
                    "tempMaxF": "86", 
                    "tempMinC": "26", 
                    "tempMinF": "79", 
                    "weatherCode": "113",  
                    "weatherDesc": [ {"value": "Sunny" } ],  
                    "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png" } ], 
                    "winddir16Point": "SE", 
                    "winddirDegree": "136", 
                    "winddirection": "SE", 
                    "windspeedKmph": "17", 
                    "windspeedMiles": "11" 
                    }, 
                    {
                    "date": "2012-06-07", 
                    "precipMM": "0.1", 
                    "tempMaxC": "30", 
                    "tempMaxF": "87", 
                    "tempMinC": "27", 
                    "tempMinF": "80", 
                    "weatherCode": "113",  
                    "weatherDesc": [ {"value": "Sunny" } ],  
                    "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png" } ], 
                    "winddir16Point": "ESE", 
                    "winddirDegree": "121", 
                    "winddirection": "ESE", 
                    "windspeedKmph": "15", 
                    "windspeedMiles": "10" 
                    }, 
                    {
                    "date": "2012-06-08", 
                    "precipMM": "2.1", 
                    "tempMaxC": "31", 
                    "tempMaxF": "87", 
                    "tempMinC": "27", 
                    "tempMinF": "81", 
                    "weatherCode": "116",  
                    "weatherDesc": [ {"value": "Partly Cloudy" } ],  
                    "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png" } ], 
                    "winddir16Point": "SSE", 
                    "winddirDegree": "166", 
                    "winddirection": "SSE", 
                    "windspeedKmph": "17", 
                    "windspeedMiles": "11" 
                    }, 
                    {
                    "date": "2012-06-09", 
                    "precipMM": "2.8", 
                    "tempMaxC": "32", 
                    "tempMaxF": "89", 
                    "tempMinC": "28", 
                    "tempMinF": "82", 
                    "weatherCode": "176",  
                    "weatherDesc": [ {"value": "Patchy rain nearby" } ],  
                    "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0009_light_rain_showers.png" } ], 
                    "winddir16Point": "SSW", 
                    "winddirDegree": "198", 
                    "winddirection": "SSW", 
                    "windspeedKmph": "17", 
                    "windspeedMiles": "11" 
                    }, 
                    {
                    "date": "2012-06-10", 
                    "precipMM": "13.0", 
                    "tempMaxC": "32", 
                    "tempMaxF": "90", 
                    "tempMinC": "28", 
                    "tempMinF": "82", 
                    "weatherCode": "116",  
                    "weatherDesc": [ {"value": "Partly Cloudy" } ],  
                    "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png" } ], 
                    "winddir16Point": "SW", 
                    "winddirDegree": "220", 
                    "winddirection": "SW", 
                    "windspeedKmph": "22", 
                    "windspeedMiles": "14" 
                    } ]
    }
}

在我的AppData.m中,代码如下所示:

    - (void)requestFinished:(ASIHTTPRequest *)request {
        NSData* responseData = [request responseData];
        NSDictionary* resultDict = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:NULL];
        NSDictionary* dataDict = [resultDict objectForKey:@"data"];
        NSArray* myArray = [dataDict objectForKey:@"weather"];

        if(weatherDataArray == nil)
            weatherDataArray = [[NSMutableArray alloc] init];
        [weatherDataArray setArray:myArray];
}

在myWeather.m中,代码如下:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        myWeatherDataCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myWeatherDataCell"];


        // get the view controller's info dictionary based on the indexPath's row
        NSDictionary* item = [[AppData sharedData].weatherDataArray objectAtIndex:indexPath.row];
        cell.maxTempLabel.text = [item objectForKey:@"tempMaxC"];
        cell.minTempLabel.text = [item objectForKey:@"tempMinC"];
        cell.dateLabel.text = [item objectForKey:@"date"];
        cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;


        NSArray* weatherIconUrl =  [item objectForKey:@"weatherIconUrl"];
        NSDictionary* value = [weatherIconUrl valueForKey:@"value"];

        NSString* urlString = [NSString stringWithFormat:@"%@",value];

        NSData* url = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]];
        cell.iconView.image = [UIImage imageWithData:url];


        NSLog(@"weatherIconUrl" "%@",urlString);

        return cell;
}

tableview可以显示

    cell.maxTempLabel.text = [item objectForKey:@"tempMaxC"];
    cell.minTempLabel.text = [item objectForKey:@"tempMinC"];
    cell.dateLabel.text = [item objectForKey:@"date"];

除了iconview.image。

我尝试使用NSlog

 NSString* urlString = [NSString stringWithFormat:@"%@",value];

它可以显示如下:

2012-11-11 11:51:46.100 MyWeather[1583:1a603] weatherIconUrl(
    "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
)
2012-11-11 11:51:46.101 MyWeather[1583:1a603] weatherIconUrl(
    "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0017_cloudy_with_light_rain.png"
)
2012-11-11 11:51:46.102 MyWeather[1583:1a603] weatherIconUrl(
    "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
)
2012-11-11 11:51:46.102 MyWeather[1583:1a603] weatherIconUrl(
    "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
)
2012-11-11 11:51:46.103 MyWeather[1583:1a603] weatherIconUrl(
    "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png"
)

但是当我尝试NSLog NSData * url时,结果为(null)。

所以我认为当“NSString * urlString”将数据传递给“NSData * url”时我陷入困境。

2 个答案:

答案 0 :(得分:0)

你可以使用JsonKit 在获得ASSIHTTPRequest的响应后解析Jdon数据并将其转换为字典 像这样

- (void) requestFinished:(ASIHTTPRequest *)request {
// Use when fetching text data


NSString *responseString = [request responseString];

// Json  dictionary
NSDictionary *resultsDictionary = [responseString objectFromJSONString];


}

如果你想要获得cloudcover,你可以说

     NSString *cloudCover =[[[resultsDictionary objectForKey:@"data"]objectForKey:@"current_condition"] objectAtIndex:0];

希望有所帮助

答案 1 :(得分:0)

正如我在评论中所说,您的变量“weatheIconURL”实际上是一个包含一个对象(字典)的数组,因此您可以使用lastObject来修复该行。所以这几行需要改为:

        NSDictionary* weatherIconUrl =  [[item objectForKey:@"weatherIconUrl"] lastObject];
        NSString* urlString = [weatherIconUrl valueForKey:@"value"];
        NSData* url = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]];
        cell.iconView.image = [UIImage imageWithData:url];