【问题标题】:Retrieving and displaying JSON data from URL (objective-C)从 URL 检索和显示 JSON 数据(objective-C)
【发布时间】:2012-10-30 23:29:59
【问题描述】:

我正在做作业,使用单例模式和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”时我陷入了困境。

【问题讨论】:

  • 看一下weatherIconUrl的结构——它有以下标点符号:[ { } ]。这对你来说代表着什么?那是字典吗?
  • [ { } ] ,我猜它的意思是数组?
  • 是的,所以 value 是一个数组而不是字典。由于它只有一个对象,您可以通过将 lastObject 放在定义值的行的末尾来提取字典。
  • 嗨,我仍然不知道如何检索 weatherIconUrl URL。你能解释一下吗,谢谢!
  • 同一个问题一遍又一遍 - URI + JSON,用它做点什么。

标签: iphone objective-c json asihttprequest


【解决方案1】:

正如我在评论中所说,您的变量“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];

【讨论】:

    【解决方案2】:

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

    - (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];
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-10
      • 2014-11-27
      • 2016-09-15
      • 1970-01-01
      • 2011-10-31
      • 1970-01-01
      • 2015-01-19
      相关资源
      最近更新 更多