iOS:JSON解析到NSArray?

问题描述:

嗨我收到以下JSON作为响应,但在尝试将其解析为NSArray时出现错误。iOS:JSON解析到NSArray?

NSArray * resp = [JSON objectForKey @“CONVCOLL”];

它抛出上述行异常。请帮助

(
{ 
     ACT1 = "<null>"; 
     ACT2 = "<null>"; 
     AUTRECERTI = "<null>"; 
     AUTRESTI = "<null>"; 
     CONVCOLL = "CCT romande du second oeuvre"; 
     DESCPHILO = "<null>"; 
     DESCSUCC = "<null>"; 
     DIRECTION = "M. Aldo Zoppi, chef d'entreprise"; 
     DISPHYGSEC = 1; 
     FILIALES = "<null>"; 
     ISO14000 = 0; 
     ISO9000 = 0;    
     ZONEACT = "La Riviera, Lavaux et Lausanne"; 
    }, 
     { 
     ACT1 = "<null>"; 
     ACT2 = "<null>"; 
     AUTRECERTI = "<null>"; 
     AUTRESTI = "<null>"; 
     CONVCOLL = "<null>"; 
     DESCPHILO = "<null>"; 
     DESCSUCC = "<null>"; 
     DIRECTION = "<null>"; 
     DISPHYGSEC = 0; 
     FILIALES = "<null>"; 
     ISO14000 = 2; 
     ISO9000 = 1;    
     ZONEACT = "<null>"; 
    }, 
     { 
     ACT1 = "<null>"; 
     ACT2 = "Volets en aluminium"; 
     AUTRECERTI = "<null>"; 
     AUTRESTI = "<null>"; 
     CONVCOLL = "<null>"; 
     DESCPHILO = "<null>"; 
     DESCSUCC = "<null>"; 
     DIRECTION = "M. Denis Zurbuchen, directeur et M. Jacques Zurbuchen, directeur d'exploitation"; 
     DISPHYGSEC = 1; 
     FILIALES = "<null>"; 
     ISO14000 = 0; 
     ISO9000 = 0;    
     ZONEACT = "<null>"; 
    } 
) 
+0

它看起来像你的JSON是一个JSONArray,而不是JSONObject,通过JSONObjects的JSONArray循环,并在每个JSONOBj等等,在上面打电话。 – wottle 2014-08-28 12:38:07

+0

阅读(并包含)例外消息或其他错误症状。 – user2864740 2014-08-28 12:40:49

+0

-1不包括确切的异常消息。 (但人们可以猜到它就像是“无法识别的选择器” - 最外层的“层”是一个数组,而不是字典)。 – 2014-08-28 12:42:47

您的网址回复为NSData的实例。所以,你可以分析它,并遍历您收到的数组:

NSArray *JSONArray = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil]; 

for(NSDictionary *entry in JSONArray) { 
    NSLog(@"CONVCOLL: %@", [entry objectForKey:@"CONVCOLL"]); 
} 
+0

他已经解析了数据他的转储是NSArray,而不是JSON – 2014-08-28 12:43:35

+0

当JSON是解析的数据,所以你有一个数组,这意味着,你必须正确地访问它的值,你必须循环条目(就像在我的代码中),或者你必须通过索引访问:[[JSON objectAtIndex:0];'。你的数组包含'NSDictionaries'。这意味着(也在我的代码中):'[[JSON objectAtIndex:0] objectForKey:@“CONVCOLL”];'' – 2014-08-28 12:47:55

试试下面的代码...

NSString *response=[request responseString]; 

    NSArray *responseArray=[response JSONValue]; 

    for(NSDictionary *dict in responseArray) 
    { 

    self.convcoll.text=[dict objectForKey:@"CONVCOLL"]; 

    } 

如果您正在使用ASIFormDataRequest解析..

NSArray * resp = [JSON [email protected]"CONVCOLL"];// this way of retrieving is not correct for above JSON.. 

希望它帮助你..