0

我有一个 twitter json Feed。问题是我在使用 jsonkit 解析 json feed 时出错。下面是 json url。我想解析下面 json feed.cud 中的值文本和 profile_background_image_url。你们帮帮我出去

[
{
    "created_at": "Mon Jul 09 21:46:49 +0000 2012",
    "id": 222446736654872580,
    "id_str": "222446736654872576",
    "text": "@mevru you have to go to  : http://t.co/pPGYijEX",
    "source": "web",
    "truncated": false,
    "in_reply_to_status_id": 222445085235752960,
    "in_reply_to_status_id_str": "222445085235752961",
    "in_reply_to_user_id": 146917266,
    "in_reply_to_user_id_str": "146917266",
    "in_reply_to_screen_name": "mevru",
    "user": {
        "id": 145125358,
        "id_str": "145125358",
        "name": "Amitabh Bachchan",
        "screen_name": "SrBachchan",
        "location": "Mumbai, India",
        "description": "Actor ... well at least some are STILL saying so !!",
        "url": "http://srbachchan.tumblr.com",
        "protected": false,
        "followers_count": 3022511,
        "friends_count": 415,
        "listed_count": 22016,
        "created_at": "Tue May 18 05:16:47 +0000 2010",
        "favourites_count": 10,
        "utc_offset": 19800,
        "time_zone": "Mumbai",
        "geo_enabled": false,
        "verified": true,
        "statuses_count": 14166,
        "lang": "en",
        "contributors_enabled": false,
        "is_translator": false,
        "profile_background_color": "BADFCD",
        "profile_background_image_url": "http://a0.twimg.com/profile_background_images/144221357/t-1.gif",
        "profile_background_image_url_https": "https://si0.twimg.com/profile_background_images/144221357/t-1.gif",
        "profile_background_tile": true,
        "profile_image_url": "http://a0.twimg.com/profile_images/2227330575/Amitji_normal.png",
        "profile_image_url_https": "https://si0.twimg.com/profile_images/2227330575/Amitji_normal.png",
        "profile_link_color": "FF0000",
        "profile_sidebar_border_color": "F2E195",
        "profile_sidebar_fill_color": "FFF7CC",
        "profile_text_color": "0C3E53",
        "profile_use_background_image": true,
        "show_all_inline_media": true,
        "default_profile": false,
        "default_profile_image": false,
        "following": null,
        "follow_request_sent": null,
        "notifications": null
    }

以下是我用来解析的ios代码

 jsonurl=[NSURL URLWithString:@"https://api.twitter.com/1/statuses/user_timeline.json?screen_name=@SrBachchan&count=10"];

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

jsonArray = [jsonData objectFromJSONString]; 

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

NSLog(@"the given text:%@",items);
story = [NSMutableArray array];
title = [NSMutableArray array];
picture = [NSMutableArray array];

for (NSDictionary *item in items )
{


}
4

4 回答 4

1

问题出在这一行:

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

正如您的代码所暗示的,它是一个数组而不是字典,因此您首先从数组中获取对象:

for (NSDictionary *item in jsonArray ) {
     NSDictionary *user = [item objectForKey:@"user"];  
     NSString *imageURLString = [user objectForKey:@"profile_image_url"];


}
于 2012-07-10T07:28:00.410 回答
0
    NSData *data = [NSData dataWithContentsOfURL:jsonurl];
    NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    if (!array) return;
    NSDictionary *dictionary = [array objectAtIndex:0];
    if (dictionary){
        NSString *text = [dictionary valueForKey:@"text"];
        NSString *profileBackgroundImageURL = [dictionary valueForKeyPath:@"user.profile_background_image_url"];
    }
于 2012-07-10T07:24:00.440 回答
0

使用jsonlint.com验证您的 JSON。错误是缺少 JSON 字符串末尾的最后一个关闭操作数 }。有2个开场,但只有1个关门。

Parse error on line 52:
...cations": null    }
----------------------^
Expecting '}', ',', ']'
于 2012-07-10T07:28:19.780 回答
0

试试这个

items = [[jsonArray objectAtIndex:0] objectForKey:@"text"];

backImage=[[[jsonArray objectAtIndex:0] objectForKey:@"user"] objectForKey:@"profile_background_image_url"] ;
于 2012-07-10T07:29:37.443 回答