2012-03-29 40 views
0

我试图解析此JSON具有 “节点” 标签的多个条目:解析JSON疑惑 - 目标C

{ 
    "nodes": [ 
     { 
     "node": { 
      "title": "Jornada del Fútbol Profesional contra el hambre", 
      "description": " 
      "image": ", 
      "fecha": , 
      "nid": ", 
      "noticia_relacionada_1_path": , 
      "noticia_relacionada_2_path": , 
      "image_small_2": 
     } 
     } 
    ] 
} 

下面是代码:

NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];  
    //data =nil; 

    NSDictionary *results = [responseString JSONValue]; 

    NSArray *noticias = [results objectForKey:@"node"]; 

    self.noticiasArray = [[NSMutableArray alloc] init]; 

    for (int i = 0; i < [noticias count]; i++) { 

     news *noticia = [[news alloc] initWithDictionary:[noticias objectAtIndex:i]]; 
     [self.noticiasArray addObject:noticia]; 
     noticia=nil; 

    } 

我得到了10元notices数组,但是当我尝试获取initWithDictionary中的内部值时:它们始终为空。

非常感谢

+1

这种 “JSON” 被打破了。孤独的引言是什么? – cHao 2012-03-29 16:17:46

+1

调试它。我的猜测是,你得到的字典有一个“node”元素作为“initWithDictionary”parm。您可能希望直接看到“标题”等元素,但是还有一层洋葱不会被剥离。 – 2012-03-29 16:20:04

+0

我怎样才能到达内部标签?谢谢 – roof 2012-03-29 16:21:54

回答

1

例子:

NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];  
//data =nil; 

NSDictionary *results = [responseString JSONValue]; 

NSDictionary *noticias = [results objectForKey:@"node"]; 

news *noticia = [[news alloc] init]; 
noticia.title = [noticias objectForKey:@"title"]; 
// do the same for the rest of the variables 
[self.noticiasArray addObject:noticia]; 
noticia=nil; 
+0

非常感谢您的帮助! – roof 2012-03-29 18:56:50