JSON数据问题iPhone

我从JSON Web服务获取这些数据

[{"identity":"DEMO","assets":[{"identity":"34DL3611","systemId":"544507"},{"identity":"34GF0512","systemId":"5290211"},{"identity":"34HH1734","systemId":"111463609"},{"identity":"34HH1736","systemId":"111463622"},{"identity":"34YCJ15","systemId":"294151155"}],"systemId":4921244}] 

我正在使用此方法来获取标识资产的值(“assets”:[{“identity”:“34DL3611”,“systemId”:“544507”}):

  vehicleList = [dict objectForKey: @"assets"]; self.listVehicles = [[NSMutableArray alloc] init]; for (NSUInteger index = 0; index < [vehicleList count]; index++) { itemDict = [vehicleList objectAtIndex: index]; [self.listVehicles addObject:[itemDict objectForKey:@"identity"]]; } 

我怎样才能得到systemId值…?

我已经尝试了这个systemId

 vehicleListID = [dict objectForKey:@"systemId"]; self.listVehiclesID =[[NSMutableArray alloc]init]; for (NSUInteger index = 0; index < [vehicleListID count]; index++) { assetsIdDict = [vehicleListID objectAtIndex: index]; [self.listVehiclesID addObject:[assetsIdDict objectForKey:@"systemId"]]; } 

但得到错误:[__NSCFNumber count]:无法识别的select发送到实例0x784c730

你得到的JSON响应是一个数组。 vehicleList应该是json响应的objectatIndex 0。

  NSDictionary *vehicleList = [[arrayLoadedFromJson objectAtIndex:0] objectForKey: @"assets"]; vehicleList = [dict objectForKey: @"assets"]; self.listVehicles = [[NSMutableArray alloc] init]; for (NSUInteger index = 0; index < [vehicleList count]; index++) { itemDict = [vehicleList objectAtIndex: index]; [self.listVehicles addObject:[itemDict objectForKey:@"identity"]]; [self.listVehicles addObject:[itemDict objectForKey:@"systemId"]]; } 

这你必须插入:在你的代码

  [self.listVehicles addObject:[itemDict objectForKey:@"systemId"]]; 

你基本上就在那里。 你只是想这可能是:

 NSString *systemId = [itemDict objectForKey:@"systemId"]; 

关于这个代码你有:

 vehicleListID = [dict objectForKey:@"systemId"]; self.listVehiclesID =[[NSMutableArray alloc]init]; for (NSUInteger index = 0; index < [vehicleListID count]; index++) { assetsIdDict = [vehicleListID objectAtIndex: index]; [self.listVehiclesID addObject:[assetsIdDict objectForKey:@"systemId"]]; } 

…那是完全错误的。 这是返回在您的示例JSON 4921244在JSONstring的最后的NSNumber 。 然后你打电话给它,但它不是一个数组,所以它崩溃。

如果你想从JSON中获得所有的值,你可以使用这个:

 NSNumber *outerIdentity = [dict objectForKey:@"identity"]; NSNumber *outerSystemId = [dict objectForKey:@"systemId"]; NSArray *vehicleList = [dict objectForKey:@"assets"]; for (NSUInteger index = 0; index < [vehicleList count]; index++) { NSDictionary *itemDict = [vehicleList objectAtIndex: index]; NSString *identity = [itemDict objectForKey:@"identity"]; NSString *systemId = [itemDict objectForKey:@"systemId"]; } 

然后做任何你想做的事情与所有这些对象。

为了在单独的数组中,你需要初始化新的NSMutableArray并添加对象。

否则,更好的方法是,用2个NSString成员声明一个新类, identitysystemId 。 从JSON中提取值并分配给类成员。 并将该对象添加到数组中。