想要使用json for ios5获取对象数组

时间:2012-05-18 12:25:09

标签: iphone objective-c

我搜索过完整的谷歌搜索。但没有找到答案。 我在解析json结果中的对象数组时遇到了问题。 我有这样的json结果:

  

[{“created_at”:“Sat Apr 28 13:36:24 +0000 2012”,       “text”:“@ LisaPrejean爱你的工作!干得好!”,       “source”:“web”,“user”:{“id”:478983313,“name”:“3rd Dimension Media”,       “created_at”:“Mon Jan 30 21:51:20 +0000 2012”,“favourites_count”:0},       “retweet_count”:0}]

&安培;从结果我想只提取“created_at”& “文本”

  
      
  • (void)connectionDidFinishLoading:(NSURLConnection *)连接   {
           NSString * jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];        NSLog(@“%@”,jsonString);        self.jsonData = nil;

    NSArray *tweetArray = [jsonString JSONValue]; 
     tweetTextArray = [NSMutableArray array];
    for (NSDictionary *tweet in tweetArray) 
    
         

    {         [tweetTextArray addObject:[tweet objectForKey:@“text”]];         [tweetCreated_atArray addObject:[tweet objectForKey:@“created_at”]];       }   }

  •   

但在线获取EXC_bad_access错误: NSArray * tweetArray = [jsonString JSONValue];

1 个答案:

答案 0 :(得分:-1)

试试这个

NSString *responseString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *data = (NSDictionary *) [parser objectWithString:responseString error:nil];
NSString *text = (NSString *) [data objectForKey:@"text"];
NSLog(@"textvalue= %@",text);
NSString * created_at = (NSString *) [data objectForKey:@"created_at"];
NSLog(@"created_at= %@",created_at);