0

I am trying to work with JSON Feed for iphone using SBJSON Framework. I did try initially with an example and it works fine. But when I did the change with my URL JSON, it couldnt find the correct key. I am not sure if I am correct since I am new in learning JSON Framework for iphone. Below is my JSON Feed :

[
{
    "place": null,
    "in_reply_to_user_id": null,
    "user": {
        "notifications": null,
        "friends_count": 37,
        "profile_text_color": "000000",
        "protected": false,
        "is_translator": false,
        "profile_sidebar_fill_color": "f1f6f9",
        "location": "St. Louis, MO",
        "name": "Annual Meeting",
        "follow_request_sent": null,
        "profile_background_tile": true,
        "show_all_inline_media": false,
        "geo_enabled": false,
        "utc_offset": -18000,
        "url": "http://www.center.org/meet",
        "id_str": "15336703",
        "following": null,
        "verified": false,
        "favourites_count": 0,
        "profile_link_color": "CB2828",
        "description": "Annual Meeting Twitterstream -- Join us in St. Louis, MO!",
        "default_profile": false,
        "created_at": "Sun Jul 06 23:26:33 +0000 2008",
        "profile_sidebar_border_color": "000000",
        "listed_count": 105,
        "statuses_count": 400,
        "profile_background_image_url_https": "https://si0.twimg.com/profile_background_images/20080706-rh1nqc98x97hyg133ji774pix1.jpg",
        "time_zone": "Eastern Time (US & Canada)",
        "profile_image_url": "http://a1.twimg.com/profile_images/AnnualMeeting2011_Twitter_Icon_normal.jpg",
        "profile_use_background_image": false,
        "profile_image_url_https": "https://si0.twimg.com/profile_images/1257786946/AnnualMeeting2011_Twitter_Icon_normal.jpg",
        "id": 15336703,
        "profile_background_color": "015196",
        "followers_count": 2311,
        "screen_name": "11",
        "contributors_enabled": false,
        "profile_background_image_url": "http://a3.twimg.com/profile_background_images/20080706-rh1nqc98x97hyg133ji774pix1.jpg",
        "default_profile_image": false,
        "lang": "en"
    },
    "in_reply_to_status_id": null,
    "text": "Acronym blog is seeking guest bloggers at #11 http://bit.ly/nlDUOe",
    "id_str": "91972636405530624",
    "favorited": false,
    "created_at": "Fri Jul 10 20:49:19 +0000 2011",
    "in_reply_to_status_id_str": null,
    "geo": null,
    "in_reply_to_screen_name": null,
    "id": 91972636405530620,
    "in_reply_to_user_id_str": null,
    "source": "<a href=\"http://cotweet.com/?utm_source=sp1\" rel=\"nofollow\">CoTweet</a>",
    "contributors": null,
    "coordinates": null,
    "retweeted": false,
    "retweet_count": 2,
    "truncated": false
},

Code to find out the correct key :

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];


NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];

NSDictionary *results = [responseString JSONFragmentValue];
NSArray *allTweets = [results objectForKey:@"user"]; // ** Error on this line, dont know what key is the correct one ? **///
[viewController setTweets:allTweets];
    [window addSubview:viewController.view];
   [window makeKeyAndVisible];
}
lifemoveson
  • 1,661
  • 8
  • 28
  • 55

3 Answers3

0

You are returning a value into an NSDictionary object, but your JSON will return an array. The JSONFragmentValue message returns id, which is why this call does not cause an error:

NSDictionary *results = [responseString JSONFragmentValue];

However, despite you declaring results as an NSDictionary it is actually an NSArray. NSArray instances will throw errors if you send them the objectForKey: message.

PS: You are using a terrifyingly old version of SBJson. Please upgrade! Lots of bugs have been fixed in newer versions.

Stig Brautaset
  • 2,602
  • 1
  • 22
  • 39
0
try this one

NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];

NSError *error;

SBJSON *json = [[SBJSON new] autorelease];




NSDictionary *results = [json objectWithString:responseString error:&error];

NSArray *allTweets = [[results objectForKey:@"user"]retain]; 

[viewController setTweets:allTweets];

[window addSubview:viewController.view];

[window makeKeyAndVisible];
0

It could be because your JSON is formatted as an array (the outer []...I'm guessing your post is incomplete since it ends in a comma) so you should have

NSArray *results = [responseString JSONValue];

and then refer to [results objectAtIndex:0]

Robot Woods
  • 5,677
  • 2
  • 21
  • 30
  • I have only posted half part of it. I have checked and verified that my JSON file is correct. I need all the data in dictionary with an array wrapping that dictionary. – lifemoveson Jul 20 '11 at 19:19
  • right, so, since it is structured as an array, turn it into one, instead of a Dictionary (see that my line begins with 'NSArray' instead of 'NSDictionary)'..then the dictionary you really want is object zero of that array – Robot Woods Jul 20 '11 at 19:22
  • did that work for you? I was just looking at this again and I realized perhaps the issue is that the line giving the error is trying to create an Array from data structured as a Dictionary...you might also need to wrap the user object in brackets to make it a one object array? but I'm just throwing that out there as a hypothetical...I don't KNOW that using key/value pairs won't work to form the array – Robot Woods Jul 21 '11 at 00:19