Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 将json nsdictionary转换为可用数组_Iphone_Json_Nsarray_Nsdictionary - Fatal编程技术网

Iphone 将json nsdictionary转换为可用数组

Iphone 将json nsdictionary转换为可用数组,iphone,json,nsarray,nsdictionary,Iphone,Json,Nsarray,Nsdictionary,我一直在努力寻找最有效的方法将JSON响应转换为可用数组,该数组可以作为UITableView数据源引用。这里的样本来自 我在使用方法进行JSON转换时没有问题,但是现在我有了一系列的NSDictionary,我似乎无法找到有效的方法将数据移动到数组中,除非使用蛮力。也就是说,我可以从嵌套的NSDictionary中获取任何值,但构建可用数组的唯一方法是分别获取每个值并将其放置到数组中的正确位置。我可以用一个单独的对象来构建一个数组,其中的字符串表示JSON响应中的每个值,但是必须有一种比使用暴

我一直在努力寻找最有效的方法将JSON响应转换为可用数组,该数组可以作为UITableView数据源引用。这里的样本来自

我在使用方法进行JSON转换时没有问题,但是现在我有了一系列的NSDictionary,我似乎无法找到有效的方法将数据移动到数组中,除非使用蛮力。也就是说,我可以从嵌套的NSDictionary中获取任何值,但构建可用数组的唯一方法是分别获取每个值并将其放置到数组中的正确位置。我可以用一个单独的对象来构建一个数组,其中的字符串表示JSON响应中的每个值,但是必须有一种比使用暴力更好的方法来实现这一点

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

是否有方法获取嵌套的NSDictionary并将嵌套的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);
以下是转换为NSDictionary的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;
            }
        );
    };
}

如果你想提高效率,我建议你不要使用TouchJSON。我强烈建议你使用谢谢,我试过JSONKit,效果很好。我仍然面临着同样的问题,即试图找到一种将嵌套的NSDictionary数据转换为可用数组的方法。蛮力是有效的,但我宁愿花一点时间用正确的方式来做,所以任何建议都将不胜感激。谢谢