2012-07-11 73 views
1

我正在查詢Facebook公共JSON供稿。我發現了一個錯誤:解析ios中的Facebook json feed錯誤

-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x9146b90 2012-07-11 10:32:25.536 iB Top 50[1789:16a03]

我試圖用

"id": "108448345915250_320180984741984" 

解析的對象標籤「信息」和「圖片」請幫幫我。

例JSON:

{ 
"data": [ 
    { 
    "id": "108448345915250_320181064741976", 
    "from": { 
     "name": "Amitabh Bachchan", 
     "category": "Actor/director", 
     "id": "108448345915250" 
    }, 
    "story": "Amitabh Bachchan edited his Website and Location.", 
    "story_tags": { 
     "0": [ 
      { 
       "id": 108448345915250, 
       "name": "Amitabh Bachchan", 
       "offset": 0, 
       "length": 16, 
       "type": "page" 
      } 
     ] 
    }, 
    "type": "status", 
    "created_time": "2012-07-10T05:30:10+0000", 
    "updated_time": "2012-07-10T05:30:10+0000", 
    "comments": { 
     "count": 0 
    } 
    }, 
    { 
    "id": "108448345915250_320180984741984", 
    "from": { 
     "name": "Amitabh Bachchan", 
     "category": "Actor/director", 
     "id": "108448345915250" 
    }, 
    "message": "Singing in the praises of this new venture \u2026 metal mike and all \u2026", 
    "picture": "http://photos-d.ak.fbcdn.net/hphotos-ak-snc7/293763_320180974741985_675837103_s.jpg", 
    "link": "http://www.facebook.com/photo.php?fbid=320180974741985&set=a.126948684065216.26767.108448345915250&type=1&relevant_count=1", 
    "icon": "http://static.ak.fbcdn.net/rsrc.php/v2/yz/r/StEh3RhPvjk.gif", 
    "type": "photo", 
    "object_id": "320180974741985", 
    "created_time": "2012-07-10T05:29:34+0000", 
    "updated_time": "2012-07-10T, 
      "shares": { 
     "count": 41 
    }, 
    "likes": { 
     "data": [ 
      { 
       "name": "Amir Samy", 
       "id": "100000377429168" 
      }, 
      { 
       "name": "Rashi Shrivastava", 
       "id": "100001002346693" 
      }, 
      { 
       "name": "Satish Wakpaijan", 
       "id": "100002129452923" 
      }, 
      { 
       "name": "Sushil Kumar", 
       "id": "100002105808620" 
      } 
     ], 
     "count": 300 
    }, 
    "comments": { 
     "count": 45 
    } 
    }, 
    ... 

的iOS代碼:

jsonurl=[NSURL URLWithString:@"https://graph.facebook.com/TheAmitabhBachchan/posts?access_token=AcessToken"]; 

jsonData=[[NSString alloc]initWithContentsOfURL:jsonurl]; 

jsonArray = [jsonData objectFromJSONString]; 

items = [jsonArray objectForKey:@"data"]; 

story = [NSMutableArray array]; 
title = [NSMutableArray array]; 
picture = [NSMutableArray array]; 

for (NSDictionary *item in items) 
{ 
    [story addObject:[item objectForKey:@"message"]]; 
    /* [title addObject:[item objectForKey:@"name"]]; 
    [picture addObject:[item objectForKey:@"picture"]];*/ 
} 
+0

是jsonArray NSDictionary ??? – 2012-07-11 06:07:15

+0

yup它的類型nsdictonary..want我想要的是解析對象「消息」和對象「圖片」.im有點絕望 – user578386 2012-07-11 06:09:29

回答

2

在行:

jsonArray = [jsonData objectFromJSONString]; 

你取出由所獲得的JSON字符串的對象。您必須將JSON字符串轉換爲正確的值。爲此使用SBJSON解析器。

這是不是你的jsonArray或項目是一本字典,你的店什麼確實在dictionary.Its值以下方法返回:

[jsonData objectFromJSONString]; 
[item objectForKey:@"message"]; 

都(jsonArray和項目)字典?其中之一是NSString,因爲你的錯誤說。正確地壓低JSON值。

然後檢查:

NSString *sss = @"https://graph.facebook.com/TheAmitabhBachchan/posts?access_token=393411130717153%7C1HFhZAByEHp89q6CPECJTFzuzfg"; 
NSURL * jsonurl=[NSURL URLWithString:[sss stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
NSData* data = [NSData dataWithContentsOfURL: jsonurl]; 
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data 
                options:kNilOptions 
                 error:nil]; 
NSArray *items = [json objectForKey:@"data"]; 
// NSDictionary *item = [items objectAtIndex:1]; 

NSMutableArray *story = [NSMutableArray array]; 

for (NSDictionary *item in items) 
{ 

    if([item objectForKey:@"message"] || [item objectForKey:@"message"] != nil || [[item objectForKey:@"message"] length]>0){ 
     [story addObject:[item objectForKey:@"message"]]; 
    } 

}    

不是每個字典的包含鍵 - 「消息」。它存在於所述陣列中的第二詞典。

+0

我使用jsonkit。 – user578386 2012-07-11 06:19:09

+0

cud u給我一個例子 – user578386 2012-07-11 06:22:13

+1

如果你的jsonArray是一個字典,那麼你的'物品'給了什麼? NSLog查看它是否是字典?來的錯誤是說您正在使用NSString的實例方法'objectForKey'。這應該用於NSDictionary/NSMutableDictionary。 – AJS 2012-07-11 06:27:18

1
NSURL * jsonurl=[NSURL URLWithString:@"https://graph.facebook.com/TheAmitabhBachchan/posts?access_token=AcessToken"]; 
NSData* data = [NSData dataWithContentsOfURL: jsonUrl]; 
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data 
                options:kNilOptions 
                 error:nil]; 
NSDictionary *items = [json objectForKey:@"data"]; 

這必須工作。現在您可以從items字典中獲取對象。

+0

cudü給我一個例子與上述jsonstring – user578386 2012-07-11 06:23:04

+0

請等待一分鐘 – 2012-07-11 06:24:48

+0

確保把你所有的時間需要... :) – user578386 2012-07-11 06:26:00

1

實際上它是一個非常BAD格式化JSON我親自測試此

NSURL *jsonurl1=[NSURL URLWithString:@"https://graph.facebook.com/TheAmitabhBachchan/posts?access_token=393411130717153%7C1HFhZAByEHp89q6CPECJTFzuzfg"]; 

NSString *jsonData=[[NSString alloc] initWithContentsOfURL:jsonurl1]; 
NSDictionary *jsonArray = [jsonData objectFromJSONString]; 
NSLog(@"Js %@", [jsonArray objectForKey:@"data"]); 

NSMutableArray *ar = [[NSMutableArray alloc] init]; 
[ar addObject:[jsonArray objectForKey:@"data"]]; 

NSLog(@"AAAA %@", [[[ar objectAtIndex:0] objectAtIndex:1] objectForKey:@"picture"]); 

並且輸出是

2012-07-11 13:20:41.987 InstagramDemp [4834:F803] AAAA http://photos-d.ak.fbcdn.net/hphotos-ak-snc7/293763_320180974741985_675837103_s.jpg