0

我正在尝试使用 SBJSON 框架为 iPhone 使用 JSON Feed。我最初确实尝试了一个示例,并且效果很好。但是当我使用我的 URL JSON 进行更改时,它找不到正确的密钥。我不确定我是否正确,因为我是学习 iPhone 的 JSON 框架的新手。下面是我的 JSON 提要:

[
{
    "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
},

找出正确密钥的代码:

- (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];
}
4

3 回答 3

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];
于 2011-07-20T17:54:04.317 回答
0

这可能是因为你的 JSON 被格式化为一个数组(外部[]......我猜你的帖子不完整,因为它以逗号结尾)所以你应该有

NSArray *results = [responseString JSONValue];

然后参考[results objectAtIndex:0]

于 2011-07-20T18:31:55.933 回答
0

您将一个值返回到一个 NSDictionary 对象中,但您的 JSON 将返回一个数组。JSONFragmentValue消息返回,这 id就是此调用不会导致错误的原因:

NSDictionary *results = [responseString JSONFragmentValue];

但是,尽管您声明results为 NSDictionary,但它实际上是 NSArray。如果您向 NSArray 实例发送消息,它们将引发错误objectForKey:

PS:你正在使用一个可怕的旧版本的 SBJson。升级!许多错误已在较新版本中得到修复。

于 2012-08-24T16:21:53.757 回答