I’m querying a Facebook public JSON feed. I’m getting an error:
-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x9146b90 2012-07-11 10:32:25.536 iB Top 50[1789:16a03]
I’m trying to parse the tag “message” and “picture” for the object with
"id": "108448345915250_320180984741984"
Please help me.
Example 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 Code:
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"]];*/
}
In the line :
you are fetching the object from the obtained JSON string. You have to convert theJSON string to its correct value. Use SBJSON parser for this.
It is not about that your jsonArray or item is a dictionary and your store the values in dictionary.Its about what does the following methods return :
Are both(jsonArray and item) dictionaries? One of them is NSString as your error says.Parse the JSON value correctly.
Then check this :
Not each of your dictionary contains the key – “message”.It is present in the second dictionary in the array.