将json nsdictionary转换为可用数组

时间:2011-09-20 02:09:56

标签: iphone json nsarray nsdictionary

我一直在尝试找到将JSON响应转换为可引用为UITableView数据源的可用数组的最有效方法。此处的示例来自World Weather Online free weather service.

使用Jonathan Wights TouchJSON方法进行JSON转换没有问题,但现在我有一个NSDray的NSarray,我似乎无法找到一种有效的方法将数据移动到数组中,除非暴力。也就是说,我可以从嵌套的NSDictionaries中获取任何值,但是我可以构建可用数组的唯一方法是单独获取每个值并将其放入数组中的正确位置。我可以使用一个单独的对象来构建一个带有表示JSON响应中每个值的字符串的数组,但必须有一个更好的方法来执行此操作而不是使用暴力。

当我尝试推荐的方法时,我将“数据”作为键,其余的输出作为值。然后我创建了一个新的objectforkey字典:@“data”,它只给了我4个键“nearest_area”,“current_condition”,“weather”和“request”的数组。如果我尝试从新创建的NSDictionary中创建一个新的NSDictionary,它会给我一个错误。

有没有办法获取嵌套的NSDictionaries并将嵌套的NSDictionary中的键和值放入nsarray?

继承代码

   - (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *webResult = [[NSString alloc] initWithData:receivedWeatherData encoding:NSUTF8StringEncoding];
    NSError *theError = NULL;
    receivedDictionary = [NSMutableDictionary dictionaryWithJSONString:webResult error:&theError];
    NSLog(@"\n%@\n",receivedDictionary);

这是转换为NSDictionarys的NSArray的输出

  {
    data =     {
        "current_condition" =         (
                        {
                cloudcover = 50;
                humidity = 44;
                "observation_time" = "01:24 AM";
                precipMM = "0.0";
                pressure = 1000;
                "temp_C" = 14;
                "temp_F" = 57;
                visibility = 10;
                weatherCode = 116;
                weatherDesc =                 (
                                        {
                        value = "Partly Cloudy";
                    }
                );
                weatherIconUrl =                 (
                                        {
                        value = "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png";
                    }
                );
                winddir16Point = NW;
                winddirDegree = 310;
                windspeedKmph = 20;
                windspeedMiles = 13;
            }
        );
        "nearest_area" =         (
                        {
                areaName =                 (
                                        {
                        value = Hobart;
                    }
                );
                country =                 (
                                        {
                        value = Australia;
                    }
                );
                latitude = "-42.920";
                longitude = "147.330";
                population = 204863;
                region =                 (
                                        {
                        value = Tasmania;
                    }
                );
                weatherUrl =                 (
                                        {
                        value = "http://free.worldweatheronline.com/weather/Australia/106511/Hobart/110625/info.aspx";
                    }
                );
            }
        );
        request =         (
                        {
                query = "Lat -42.92 and Lon 147.33";
                type = LatLon;
            }
        );
        weather =         (
                        {
                date = "2011-09-20";
                precipMM = "2.4";
                tempMaxC = 12;
                tempMaxF = 54;
                tempMinC = 7;
                tempMinF = 45;
                weatherCode = 116;
                weatherDesc =                 (
                                        {
                        value = "Partly Cloudy";
                    }
                );
                weatherIconUrl =                 (
                                        {
                        value = "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png";
                    }
                );
                winddir16Point = W;
                winddirDegree = 277;
                winddirection = W;
                windspeedKmph = 23;
                windspeedMiles = 14;
            }
        );
    };
}

0 个答案:

没有答案
相关问题