天気APIからデータをダウンロードするための非常に基本的な方法を学んでいます。基本的にチュートリアルに従おうとしています。
URL を使用して、JSON 形式のデータを辞書にダウンロードできます。次に、配列に入れます。
私の質問は、配列内の項目の特定の値をどのように読み取るかです。
たとえば、配列の NSLOG を実行すると、次のようになります... 55 個の項目があるため、カット アンド ペーストしたのは 2 つだけです。
だから私の質問は、この配列の特定の値を取得するにはどうすればよいですか?
2013-03-18 14:37:57.576 LocalWeatherV3[1220:c07] loans: {
UV = 2;
"dewpoint_c" = "-4";
"dewpoint_f" = 24;
"dewpoint_string" = "24 F (-4 C)";
"display_location" = {
city = "Jersey City";
country = US;
"country_iso3166" = US;
elevation = "47.00000000";
full = "Jersey City, NJ";
latitude = "40.75180435";
longitude = "-74.05393982";
state = NJ;
"state_name" = "New Jersey";
zip = 07097;
};
estimated = {
};
"feelslike_c" = 2;
"feelslike_f" = 35;
"feelslike_string" = "35 F (2 C)";
"forecast_url" = "http://www.wunderground.com/US/NJ/
ここに.mの一部があります
- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
NSArray* latestLoans = [json objectForKey:@"current_observation"]; //2
NSLog(@"loans: %@", latestLoans); //3
// 1) Get the latest loan
//NSDictionary* loan = [latestLoans objectAtIndex:1];
NSInteger counter = [latestLoans count];
前もって感謝します!!
だから私がこれをするとき
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
マウスをローカル ウォッチに合わせると、わかりました
json NSDictionary * 0x08d62d40
[0] key/value pair
key id 0x08d61cf0
value id 0x08d62100
[1] key/value pair
key id 0x08d62150
value id 0x08d633a0
それから私はします
NSArray* latestLoans = [json objectForKey:@"current_observation"]; //2
NSLog(@"loans: %@", latestLoans); //3
私が欲しい項目の 1 つは、すべてのデータが表示される "latestloans" にあります。だから私はアイテムの1つをつかむ方法を理解できません