2011-12-31 6 views
2

iOS 5の新機能を使用してJSONを解析しています。なぜキー値ペアが得られないのかわかりません。 "aStr"(データの文字列表現)は出力ウィンドウに正しいJSONを配置していますが、 "dicData"には何も表示されず、エラーもありません。NSJSONSerializationがキーバリューペアを作成していません

ご協力いただきまして誠にありがとうございます。

これは私があなたのJSONをこのようにフォーマットされ

NSError *error = nil; 
    NSData *data = [NSData dataWithContentsOfURL:[NSURL  URLWithString:@"http://www.macscandal.com/?json=get_post&post_id=436"]]; 

NSString* aStr; 
aStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; 

//NSLog(@"data = %@",aStr); 
NSDictionary *dicData = [NSJSONSerialization 
          JSONObjectWithData:data 
          options:NSJSONReadingAllowFragments 
          error:&error]; 
//NSLog(@"error = %@",error); 
NSString *title = [dicData objectForKey:@"title"]; 

答えて

1

を使用していますものです:

{ 
    "status": "ok", 
    "post": { 
    "id": 436, 
    "type": "post", 
    "slug": "foxconn-likely-to-get-assembly-contract-for-apple-tv-set", 
    "url": "http:\/\/www.macscandal.com\/index.php\/2011\/12\/28\/foxconn-likely-to-get-assembly-contract-for-apple-tv-set\/", 
    "status": "publish", 
    "title": "Foxconn Likely to get Assembly Contract for Apple TV Set", 
... 

を私はNSJSONSerializationを使用していないが、どのように私は希望ちょうどこのALG自然JSONの構文解析を、以下のことですそれを取得しようとします。

NSDictionary *dicData = [NSJSONSerialization 
          JSONObjectWithData:data 
          options:NSJSONReadingAllowFragments 
          error:&error]; 

NSDictionary *postData = [dicData objectForKey:@"post"]; 
NSString *title = [postData objectForKey:@"title"]; 

EDIT

するだけの簡単なチェック方法:答えシプリアンため

-(void)check{ 

    NSError *error = nil; 
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.macscandal.com/?json=get_post&post_id=436"]]; 

    NSDictionary *dicData = [NSJSONSerialization 
          JSONObjectWithData:data 
          options:NSJSONReadingAllowFragments 
          error:&error]; 

    NSDictionary *postData = [dicData objectForKey:@"post"]; 
    NSString *title = [postData objectForKey:@"title"]; 

    NSLog(@"%@", title); 
} 
+0

おかげで、私はまだ0のキー/値のペアを取得していますか?何か案が? – Leo

+0

その理由はわかりません。私はそれをチェックしようとします。 – Cyprian

+0

私はそれを試してみましたが、それは私のために働いた: '2011-12-31 20:42:04.595 UIViewデモ[2406:f803] FoxconnはApple TVセットのアセンブリ契約を取得する可能性があります' – Cyprian

関連する問題