繁体   English   中英

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

[英]Retrieving and displaying JSON data from URL (objective-C)

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

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

  { "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”时,我陷入了困境。

您可以使用JsonKit解析Jdon数据并将其转换为字典,然后从ASSIHTTPRequest获得这样的响应

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


NSString *responseString = [request responseString];

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


}

在您的情况下,如果您想获得云覆盖,您可能会说

     NSString *cloudCover =[[[resultsDictionary objectForKey:@"data"]objectForKey:@"current_condition"] objectAtIndex: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];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM