0

現在の温度を表示するために、SBJson でいくつかの json データを解析しようとしています。このチュートリアルのサンプル コードは完璧に機能します:チュートリアル: JSON のフェッチと解析

コードを json フィードに変更すると、null が返されます。私は JSON の初心者ですが、見つけたすべてのチュートリアルとドキュメントに従いました。使用した json ソース: JSON ソース

sbjson を使用した私のコード:

NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
self.responseData = nil;

NSArray* currentw = [(NSDictionary*)[responseString JSONValue] objectForKey:@"current_weather"];

//choose a random loan
NSDictionary* weathernow = [currentw objectAtIndex:0];

//fetch the data
NSNumber* tempc = [weathernow objectForKey:@"temp_C"];
NSNumber* weatherCode = [weathernow objectForKey:@"weatherCode"];


NSLog(@"%@ %@", tempc, weatherCode);

もちろん、他の sbjson コードは既に実装しています。

4

3 に答える 3

0

NSJSONSerializationの代わりに使用しJSONValueます。

NSData* data = [responseString dataUsingEncoding:NSUTF8StringEncoding];
            NSDictionary* jsonDict = [NSJSONSerialization
                                      JSONObjectWithData:data
                                      options:kNilOptions
                                      error:&error];
 NSLog(@"jsonDict:%@",jsonDict);

リンクにはcurrent_weatherキーがありません。

NSString* tempc = [[[[jsonDict objectForKey:@"data"] objectForKey:@"current_condition"] objectAtIndex:0] objectForKey:@"temp_C"];
于 2013-04-18T10:49:47.437 に答える
0

指定されたリンクは、current_weather パラメーターなしで json を返します。current_condition パラメータしかありません。これを確認してください。

于 2013-04-18T10:50:14.313 に答える