开发者

convert json nsdictionary into useable array

I have been trying to find the most efficient way to convert a JSON response into a useable array that can be referenced as a UITableView data source. The sample here is from the World Weather Online free weather service.

I have no problems in doing the JSON conversion using Jonathan Wights TouchJSON method but now that I have an NSarray of NSDictionaries I can't seem to find an efficient way to move the data into an array except by brute force. That is I can get any value from the nested NSDictionaries but the only way I can build a usable array is to take each value individually and place it into the correct location in my array. I can do that with a seperate object that builds an array with strings that represent each of the values in the JSON reponse, but there must be a better way of doing this than having to use brute force.

When I try the recomended methods I get "data" as the key and the rest of the output as the value. I then make a new dictionary of the objectforkey:@"data" and it only gives me an array of the 4 keys "nearest_area", "current_condition", "weather" and "request". And if I try and make a new NSDictionary from the newly created NSDictionary it gives me an error.

Is there a way to take the nested NSDictionaries and place the keys and values in the nested NSDictionary into an nsarray?

Heres the code

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

Here is the output converted into an NSArray of NSDictionarys

  {
    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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜