2012-07-10 61 views
0

我有一个twitter json Feed.the的东西是即时消息得到错误,而解析json feed .im使用jsonkit.below是json url.i想解析值文本和profile_background_image_url在下面的json feed.cud u人帮我出解析iphone中的json

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

下面是我用来分析

jsonurl=[NSURL URLWithString:@"https://api.twitter.com/1/statuses/[email protected]&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) 
{ 


} 
+1

什么是错误您收到? – rckoenes 2012-07-10 07:20:51

+0

终止应用程序由于未捕获的异常'NSInvalidArgumentException',原因:' - [JKArray objectForKey:]:无法识别的选择器发送到实例0x9147780' – user578386 2012-07-10 07:23:22

+0

什么是错误? – Tendulkar 2012-07-10 07:23:29

回答

1

的问题是在这条线的IOS代码:

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

当你的代码表明它是一个数组不是字典因此你可以从阵列的第一抢对象:

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


} 
+0

@ rckones:谢谢,完全working.i需要解析“profile_image_url”。这是在里面对象“用户”.cud帮我这个 – user578386 2012-07-10 07:42:14

+0

我已经改变了我的答案,包括如何获得子项目。 – rckoenes 2012-07-10 07:44:58

+0

谢谢,完全合作 – user578386 2012-07-10 07:47:34

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"]; 
    } 
+0

我会试一试..谢谢 – user578386 2012-07-10 07:25:58

0

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

Parse error on line 52: 
...cations": null } 
----------------------^ 
Expecting '}', ',', ']' 
+0

这只是我给出的示例代码 – user578386 2012-07-10 07:31:11

+0

Yah,我对该示例给予了评论:)只是为了确保数据格式上的错误不会节省很多次。 – Shinigamae 2012-07-10 07:34:19

0

试试这个

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

backImage=[[[jsonArray objectAtIndex:0] objectForKey:@"user"] objectForKey:@"profile_background_image_url"] ;