我从JSON Web服务获取此数据
List ARRAY: (
{
assets = (
{
identity = 34DL3611;
systemId = 544507;
},
{
identity = 34GF0512;
systemId = 5290211;
},
{
identity = 34HH1734;
systemId = 111463609;
},
{
identity = 34HH1736;
systemId = 111463622;
},
{
identity = 34YCJ15;
systemId = 294151155;
}
);
identity = DEMO;
systemId = 4921244;
})
使用此代码:
NSArray *list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"Response data: %@", responseString);
NSLog(@"List ARRAY: %@", list);
NSDictionary *dict = [list objectAtIndex: 0];
NSMutableArray *vehicleGroups = [dict objectForKey:@"identity"];
NSLog(@"Vehicle Groups: %@", vehicleGroups);
以下是我正在使用的选择器代码:
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent :(NSInteger)component {
return [vehicleGroups count];}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{return nil;}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
}
应用程序在
行崩溃return [vehicleGroups count];
numberOfRowsInComponent
的委托方法pickerView
。我没有得到 - 我为什么要面对这个问题?
//代码////
NSArray *list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"Response data: %@", responseString);
NSLog(@"List ARRAY: %@", list);
NSDictionary *dict = [list objectAtIndex: 0];
vehicleList = [dict objectForKey: @"assets"];
self.vehicleGroups = [[NSMutableArray alloc] init];
vehicleGroups = [dict objectForKey:@"identity"];
NSLog(@"Vehicle Groups: %@", vehicleGroups);
NSString *identity = [dict objectForKey: @"identity"];
NSString *systemid = [dict objectForKey:@"systemId"];
self.listVehicles = [[NSMutableArray alloc] init];
self.listVehiclesID =[[NSMutableArray alloc]init];
NSLog(@"GroupOfVehicles: %@", groupOfVehicles);
for (NSUInteger index = 0; index < [vehicleList count]; index++) {
itemDict = [vehicleList objectAtIndex: index];
[self.listVehicles addObject:[itemDict objectForKey:@"identity"]];
[self.listVehiclesID addObject:[itemDict objectForKey:@"systemId"]];
}
NSLog(@"Group Name: %@", identity);
NSLog(@"Assets: %@", listVehicles);
NSLog(@"Assets System ID: %@", listVehiclesID);
NSLog(@"GroupSystemID: %@", systemid);
答案 0 :(得分:7)
您必须首先初始化数组
NSDict *list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"List Dict: %@", list);
NSMutableArray *temp = list[@"assets"];
NSMutableArray *vehicleGroups = [NSMutableArry array];
vehicleGroups = temp[0][@"identity"];
NSLog(@"Vehicle Groups: %@", vehicleGroups);
答案 1 :(得分:2)
如果他们遇到同样的问题,回答我自己的问题就会引导他人。实际上我在这里做的是获得所需的身份数组:DEMO如下:
vehicleGroups =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"Response data: %@", responseString);
NSLog(@"List ARRAY: %@", vehicleGroups);
NSDictionary *dict1 = [vehicleGroups objectAtIndex: 0];
NSString *identity1 = [dict objectForKey: @"identity"];
NSLog(@"dictNEW: %@", dict1);
groupOfVehicles = [[NSMutableArray alloc] init];
for (NSUInteger index = 0; index < [vehicleGroups count]; index++) {
itemDict = [vehicleGroups objectAtIndex: index];
[groupOfVehicles addObject:[itemDict objectForKey:@"identity"]];
}
NSLog(@"Group Name NEW: %@", identity1);
NSLog(@"Groups NEW: %@", groupOfVehicles);
使用该代码后,我已正确获取所需数据的数组
答案 2 :(得分:0)
将数组保留为
在[vehicleGroups retain]
之后 vehicleGroups = [dict objectForKey:@"identity"];
答案 3 :(得分:0)
NSDictionary *dicts = [list objectAtIndex: 0];
NSMutableArray *vehicleGroups = [[NSMutableArray alloc] init];
for (NSDictionary *dict in dicts)
{
NSString* identity = [dict objectForKey:@"identity"];
[vehicleGroups addObject:identity];
}
NSLog("count is %d",[vehicleGroups count]);
试一试:)
答案 4 :(得分:0)
您的JSON数据不是JSON。
{
"assets" : [
{
"identity" : "34DL3611",
"systemId" : 544507
},
....
{
"identity" = "34YCJ15",
"systemId" = 294151155
}
],
"identity" = "DEMO",
"systemId" = 4921244
}
这是您在JSON中描述的数据的一个可能示例。
答案 5 :(得分:0)
NSDictionary *dict = [list objectAtIndex: 0];
NSMutableArray *vehicleGroups = [dict objectForKey:@"identity"];
在我看来,dict
这里有一个包含三个条目的字典:assets,identity和systemId。在这个级别,identity的值只是一个字符串,“DEMO”,但是你试图将它分配给一个可变数组。我想你想要这样的东西:
NSDictionary *dict = [list objectAtIndex: 0];
NSArray *assets = [dict objectForKey:@"assets"];
NSArray *vehicleGroups = [assets objectForKey:@"identity"];
(将-objectForKey:
发送到数组是可以的 - 您将获得与接收器中每个对象的给定键对应的对象数组。)