0

SBJSON フレームワークを使用して iPhone 用の JSON フィードを操作しようとしています。私は最初に例を試してみましたが、うまくいきました。しかし、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

JSONが配列としてフォーマットされている可能性があります(外側[]...カンマで終わるため、投稿は不完全だと思います)。

NSArray *results = [responseString JSONValue];

を参照してください。[results objectAtIndex:0]

于 2011-07-20T18:31:55.933 に答える
0

NSDictionary オブジェクトに値を返していますが、JSON は配列を返します。JSONFragmentValueメッセージは を返します。 idこれが、この呼び出しがエラーを引き起こさない理由です。

NSDictionary *results = [responseString JSONFragmentValue];

ただし、resultsNSDictionary として宣言しているにもかかわらず、実際には NSArray です。メッセージを送信すると、NSArray インスタンスはエラーをスローしobjectForKey:ます。

PS:恐ろしいほど古いバージョンの SBJson を使用しています。アップグレードしてください!新しいバージョンでは多くのバグが修正されています。

于 2012-08-24T16:21:53.757 に答える
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 に答える