【问题标题】:Objective C "for" loop to parse a JSON array用于解析 JSON 数组的目标 C“for”循环
【发布时间】:2015-11-26 15:45:04
【问题描述】:

我在解析以下 json 数组时遇到问题,我想我的问题是我不知道如何使“for”循环通过 json 数组。嗯,这是 Json:

 [
  {
   "clavemateria": "TIB1025   ",
   "nombremateria": "PROGRAMACION WEB                                                                     ",
   "nombrecorto": "PROGRAMACION WEB    ",
   "clavegrupo": "9A  ",
   "horalunes": "10001100",
   "horamartes": "10001100",
   "horamiercoles": "10001100",
   "horajueves": "10001100",
   "horaviernes": "10001100"
   },
  {
   "clavemateria": "AEB1011   ",
   "nombremateria": "DESARROLLO DE APLICACIONES PARA DISPOSITIVOS MOVILES                                ",
    "nombrecorto": "DES. A. DIS. MOVILES",
    "clavegrupo": "9A  ",
    "horalunes": "11001200",
    "horamartes": "11001200",
    "horamiercoles": "11001200",
    "horajueves": "11001200",
    "horaviernes": "11001200"
   },
   {
    "clavemateria": "AEB1055   ",
    "nombremateria": "PROGRAMACION WEB                                                                      ",
    "nombrecorto": "PROG. WEB           ",
    "clavegrupo": "fD  ",
    "horalunes": "10001100",
    "horamartes": "10001100",
    "horamiercoles": "10001100",
    "horajueves": "10001100",
    "horaviernes": "10001100"
   }
 ]

这是我的代码:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
   NSDictionary *jsonRecibido = [NSJSONSerialization JSONObjectWithData:datosWeb options:kNilOptions error:NULL];
   NSString *clavemateria, *nombremateria, *nombrecorto, *clavegrupo, *horalunes, *horamartes, *horamiercoles, *horajueves, *horaviernes;



    for(id elemento in jsonRecibido)
    {
      clavemateria = [elemento objectForKey:@"clavemateria"];
      nombremateria = [elemento objectForKey:@"nombremateria"];
      nombrecorto = [elemento objectForKey:@"nombrecorto"];
      clavegrupo = [elemento objectForKey:@"clavegrupo"];
      horalunes = [elemento objectForKey:@"horalunes"];
      horamartes = [elemento objectForKey:@"horamartes"];
      horamiercoles = [elemento objectForKey:@"horamiercoles"];
      horajueves = [elemento objectForKey:@"horajueves"];
      horaviernes = [elemento objectForKey:@"horaviernes"];

      //Objeto para guardar las variables.
      FatherClass *group = [[FatherClass alloc]init];
      [group setSubjectKey:clavemateria];
      [group setLongNameSubject:nombremateria];
      [group setShortNameSubject:nombrecorto];
      [group setGroupKey:clavegrupo];
      [group setMondaySchedule:horalunes];
      [group setThursdaySchedule:horajueves];
      [group setWednesdaySchedule:horamiercoles];
      [group setTuesdaySchedule:horamartes];
      [group setFridaySchedule:horaviernes];

      [groupArray addObject:group];
   }
 }

在for循环中,我将组对象添加到我的Father类中,那么,通过这个数组的正确方法是什么?

【问题讨论】:

  • 这是一个字典数组;所以NSDictionary *jsonRecibido = 是错误的。否则代码应该可以工作。
  • 那么@trojanfoe会怎么样
  • 改用NSArray

标签: objective-c arrays json for-loop


【解决方案1】:

这里有很多旧式语法和其他可以改进的东西...

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // this is an NSArray not an NSDictionary
    NSArray *jsonRecibido = [NSJSONSerialization JSONObjectWithData:datosWeb options:kNilOptions error:NULL];

    // The elements of the array ARE NSDictionary
    for(NSDictionary *elemento in jsonRecibido)
    {
        // Use modern Obj-C syntax to get value from dictionary
        // Place them directly in the array.
        // No need to store them in separate strings.

        //Objeto para guardar las variables.
        FatherClass *group = [FatherClass new];
        [group setSubjectKey:elemento[@"clavemateria"]];
        [group setLongNameSubject:elemento[@"nombremateria"]];
        [group setShortNameSubject:elemento[@"nombrecorto"]];
        [group setGroupKey:elemento[@"clavegrupo"]];
        [group setMondaySchedule:elemento[@"horalunes"]];
        [group setThursdaySchedule:elemento[@"horamartes"]];
        [group setWednesdaySchedule:elemento[@"horamiercoles"]];
        [group setTuesdaySchedule:elemento[@"horajueves"]];
        [group setFridaySchedule:elemento[@"horaviernes"]];

        [groupArray addObject:group];
    }
}

这应该可以工作并且还可以缩短代码并更容易看到正在发生的事情:D

【讨论】:

    【解决方案2】:

    交换

    NSDictionary *jsonRecibido = [NSJSONSerialization JSONObjectWithData:datosWeb options:kNilOptions error:NULL];
    

    NSArray *jsonRecibido = [NSJSONSerialization JSONObjectWithData:datosWeb options:kNilOptions error:NULL];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-04
      相关资源
      最近更新 更多