2014-12-08 2 views
-2

NSDictionnary에서 json 데이터로 특정 값을 가져오고 싶습니다. "windspeedMiles"키의 첫 번째 값 (예 : current_condition) 만 가져 오려고합니다. 내가 가져했습니다 내 JSON 데이터는 다음과 같습니다 여기 NSDictionnary로 특정 Json 데이터 가져 오기

{ 
    "data": { 
    "nearest_area": [ 
     { 
     "distance_miles": "0.0", 
     "latitude": "54.500", 
     "longitude": "-8.500" 
     } 
    ], 
    "request": [ 
     { 
     "query": "Lat 54.50 and Lon -8.50", 
     "type": "LatLon" 
     } 
    ], 
    "weather": [ 
     { 
     "date": "2014-12-09", 
     "hourly": [ 
      { 
      "cloudcover": "59", 
      "humidity": "84", 
      "precipMM": "0.1", 
      "pressure": "1024", 
      "sigHeight_m": "3.1", 
      "swellDir": "290", 
      "swellHeight_m": "3.0", 
      "swellPeriod_secs": "12.6", 
      "tempC": "5", 
      "tempF": "41", 
      "time": "0", 
      "visibility": "10", 
      "waterTemp_C": "12", 
      "waterTemp_F": "53", 
      "weatherCode": "116", 
      "weatherIconUrl": [ 
       { 
       "value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png" 
       } 
      ], 
      "winddir16Point": "SSW", 
      "winddirDegree": "204", 
      "windspeedKmph": "34", 
      "windspeedMiles": "21" 
      }, 
      { 
      "cloudcover": "84", 
      "humidity": "87", 
      "precipMM": "0.1", 
      "pressure": "1020", 
      "sigHeight_m": "0.6", 
      "swellDir": "240", 
      "swellHeight_m": "0.1", 
      "swellPeriod_secs": "12.4", 
      "tempC": "6", 
      "tempF": "42", 
      "time": "300", 
      "visibility": "10", 
      "waterTemp_C": "12", 
      "waterTemp_F": "53", 
      "weatherCode": "119", 
      "weatherIconUrl": [ 
       { 
       "value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0003_white_cloud.png" 
       } 
      ], 
      "winddir16Point": "SSW", 
      "winddirDegree": "193", 
      "windspeedKmph": "45", 
      "windspeedMiles": "28" 
      }, 
      { 
      "cloudcover": "75", 
      "humidity": "91", 
      "precipMM": "0.4", 
      "pressure": "1016", 
      "sigHeight_m": "3.1", 
      "swellDir": "290", 
      "swellHeight_m": "3.0", 
      "swellPeriod_secs": "11.3", 
      "tempC": "7", 
      "tempF": "44", 
      "time": "600", 
      "visibility": "2", 
      "waterTemp_C": "12", 
      "waterTemp_F": "53", 
      "weatherCode": "266", 
      "weatherIconUrl": [ 
       { 
       "value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0017_cloudy_with_light_rain.png" 
       } 
      ], 
      "winddir16Point": "SSW", 
      "winddirDegree": "193", 
      "windspeedKmph": "54", 
      "windspeedMiles": "34" 
      }, 
      { 
      "cloudcover": "0", 
      "humidity": "93", 
      "precipMM": "0.8", 
      "pressure": "1011", 
      "sigHeight_m": "1.0", 
      "swellDir": "250", 
      "swellHeight_m": "0.2", 
      "swellPeriod_secs": "10.6", 
      "tempC": "16", 
      "tempF": "61", 
      "time": "900", 
      "visibility": "10", 
      "waterTemp_C": "12", 
      "waterTemp_F": "53", 
      "weatherCode": "113", 
      "weatherIconUrl": [ 
       { 
       "value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png" 
       } 
      ], 
      "winddir16Point": "SSW", 
      "winddirDegree": "202", 
      "windspeedKmph": "62", 
      "windspeedMiles": "39" 
      }, 
      { 
      "cloudcover": "98", 
      "humidity": "95", 
      "precipMM": "6.6", 
      "pressure": "1006", 
      "sigHeight_m": "3.7", 
      "swellDir": "290", 
      "swellHeight_m": "3.6", 
      "swellPeriod_secs": "11.0", 
      "tempC": "18", 
      "tempF": "65", 
      "time": "1200", 
      "visibility": "7", 
      "waterTemp_C": "12", 
      "waterTemp_F": "53", 
      "weatherCode": "302", 
      "weatherIconUrl": [ 
       { 
       "value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0018_cloudy_with_heavy_rain.png" 
       } 
      ], 
      "winddir16Point": "SW", 
      "winddirDegree": "220", 
      "windspeedKmph": "64", 
      "windspeedMiles": "40" 
      }, 
      { 
      "cloudcover": "89", 
      "humidity": "95", 
      "precipMM": "8.3", 
      "pressure": "1003", 
      "sigHeight_m": "4.3", 
      "swellDir": "290", 
      "swellHeight_m": "4.0", 
      "swellPeriod_secs": "10.9", 
      "tempC": "19", 
      "tempF": "65", 
      "time": "1500", 
      "visibility": "5", 
      "waterTemp_C": "12", 
      "waterTemp_F": "53", 
      "weatherCode": "308", 
      "weatherIconUrl": [ 
       { 
       "value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0018_cloudy_with_heavy_rain.png" 
       } 
      ], 
      "winddir16Point": "SW", 
      "winddirDegree": "234", 
      "windspeedKmph": "60", 
      "windspeedMiles": "38" 
      }, 
      { 
      "cloudcover": "49", 
      "humidity": "73", 
      "precipMM": "1.5", 
      "pressure": "1004", 
      "sigHeight_m": "5.7", 
      "swellDir": "290", 
      "swellHeight_m": "5.3", 
      "swellPeriod_secs": "10.8", 
      "tempC": "7", 
      "tempF": "45", 
      "time": "1800", 
      "visibility": "10", 
      "waterTemp_C": "12", 
      "waterTemp_F": "53", 
      "weatherCode": "353", 
      "weatherIconUrl": [ 
       { 
       "value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0009_light_rain_showers.png" 
       } 
      ], 
      "winddir16Point": "W", 
      "winddirDegree": "261", 
      "windspeedKmph": "66", 
      "windspeedMiles": "41" 
      }, 
      { 
      "cloudcover": "58", 
      "humidity": "73", 
      "precipMM": "0.5", 
      "pressure": "1005", 
      "sigHeight_m": "7.2", 
      "swellDir": "290", 
      "swellHeight_m": "6.7", 
      "swellPeriod_secs": "10.6", 
      "tempC": "5", 
      "tempF": "42", 
      "time": "2100", 
      "visibility": "10", 
      "waterTemp_C": "12", 
      "waterTemp_F": "53", 
      "weatherCode": "353", 
      "weatherIconUrl": [ 
       { 
       "value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0009_light_rain_showers.png" 
       } 
      ], 
      "winddir16Point": "W", 
      "winddirDegree": "260", 
      "windspeedKmph": "79", 
      "windspeedMiles": "49" 
      } 
     ], 
     "maxtempC": "19", 
     "mintempC": "5" 
     } 
    ] 
    } 
} 

내 코드입니다 :

Reports.m :

#import "Reports.h" 

@interface Reports() 

@end 

@implementation Reports 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

     NSURL *url = [NSURL URLWithString:@"http://api.worldweatheronline.com/free/v2/weather.ashx?q=Bundoran&format=json&num_of_days=5&key=e1caa114b7fba6f5bcea272e1fc77"]; 

     NSData *data = [NSData dataWithContentsOfURL:url]; 
     NSDictionary *dico = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; 

     NSDictionary *wind = [dico objectForKey:@"windspeedMiles"]; 
     NSLog(@"The current windspeedMiles is : %@", wind); 

} 


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 



@end 
+3

좋아. 질문이 뭐야? – Madbreaks

+0

@Madbreaks "windspeedMiles"키의 첫 번째 값 (예 : current_condition) 만 가져 오려고합니다. 내 NSLog가 "nil"을 보내면 current_condition의 값 즉, "19"가 표시됩니다. – Viny76

+0

그럼 JSON 레이아웃이 있습니다. Objective-C 객체 레이아웃은 NSJSONSerialization으로 파싱 한 후에도 동일합니다. 그냥 레벨을 통해 액세스하고 데이터를 얻을. –

답변

0

귀하의 의도 된 키 중첩 NSDictionary 객체에있는, 그래서 당신이 사용하여 그것을 검색 할 수 없습니다 :

NSDictionary *wind = [dico objectForKey:@"windspeedMiles"]; 
NSLog(@"The current windspeedMiles is : %@", wind); 

사용 :

NSDictionary *wind = [dico[@"data"][@"condition"] objectAtIndex:0]; 
NSLog(@"The current windspeedMiles is : %@", wind[@"windspeedMiles"]); 
+0

Downvoter, 이유를 말씀해주십시오. –

+0

정확히 내가 원하는 것! 괜찮아, 투표하지 마! – Viny76

+0

@ Viny76 : 답변이 정확하다는 것을 알고 있습니다. 그리고 downvoter도 대답이 괜찮은지 알고 있습니다. 충분한 명성을 얻은 일부 사람들이 있습니다. 그들은 사디스트 정신을 시작하고 모든 사람들, 특히 새로운 SO 사용자들을 혼란스럽게 만듭니다. 그것에 대해 아무 말도 할 수 없습니다. 나는 그것이 당신을 위해 일했다는 것을 듣게 행복하다 :) –

1

당신이, 당신이하고자 게시 된 구조에서 특정 값을 잡기 위해 액세스 :

[[[[dico objectForKey:@"data"] objectForKey:@"current_condition"] objectAtIndex:0] objectForKey:@"windspeedMiles"] 

구조를 살펴 봐야합니다. JSONObjectWithData으로 사전을 만들면 모든 중괄호 세트에 대해 NSDictionary, 모든 사각형 세트에 대해 NSArray을 얻을 수 있습니다.

당신의 JSON에서 모든 "데이터"의 하위 오브젝트이기 때문에

, 내가 만드는 것이 좋습니다 원래대로 NSDictionary : 당신은 "데이터"사전을 참조 유지할 필요가 없습니다

NSDictionary *dico = [[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil] objectForKey:@"data"]; 

이 방법 .

+0

downvote의 이유는 무엇입니까? 그의 대답은 맞습니까? –