【问题标题】:JSON array for picker view iPhone选择器视图 iPhone 的 JSON 数组
【发布时间】:2012-02-12 00:45:29
【问题描述】:

我从 JSON 网络服务获取这些数据

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];

pickerView 的委托方法 numberOfRowsInComponent。我不明白 - 为什么我会遇到这个问题?

//代码////

    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);

【问题讨论】:

  • 日志中没有任何内容,它给我一条消息:程序接收信号:EXC_BAD_ACCESS
  • 显示选择器的代码并提及应用程序崩溃的位置
  • 我已经编辑了我的问题并添加了pickerView的代码
  • 在那里放一个断点并检查它是否对车辆组有价值。
  • @hiren443 响应正常我已经在获取资产数组的数据,并且它正在表中使用,我认为问题是我得到的是字符串而不是数组...

标签: iphone objective-c ios xcode json


【解决方案1】:

你必须先初始化你的数组

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);

【讨论】:

  • 现在已经初始化了,但是问题依旧
  • 然后 NSLog 你的字典并检查它的显示或 nil 首先 NSLog 你的字典
  • 您需要车辆组中的所有身份信息吗?
  • List ARRAY: ( {assets = ({ identity = 34DL3611; systemId = 544507;},{....}....); identity = DEMO; systemId = 4921244;} 这里我m 获取 assets 和 identity 的所有值(在 assets 数组中),但是 list Array 中的 identity = DEMO 的值应该显示在选择器视图中,它不起作用
  • 请再次检查我的问题,我已对其进行了编辑并添加了代码
【解决方案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);

使用该代码后,我已正确获取所需数据的数组

【讨论】:

    【解决方案3】:

    将数组保留为

    [vehicleGroups retain]vehicleGroups = [dict objectForKey:@"identity"]; 之后

    【讨论】:

    • 我已经尝试过了,现在控制台出现了这个错误:-[__NSCFString count]: unrecognized selector sent to instance
    【解决方案4】:
    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]);
    

    试一试:)

    【讨论】:

    • 得到错误 [__NSCFString addObject:]: unrecognized selector sent to instance
    【解决方案5】:

    您的 JSON 数据不是 JSON

    {
        "assets" : [
            {
                "identity" : "34DL3611",
                "systemId" : 544507
            },
            ....
            {
                "identity" = "34YCJ15",
                "systemId" = 294151155
            }
        ],
       "identity" = "DEMO",
       "systemId" = 4921244
    }
    

    这是您的数据的一个可能示例,它是用 JSON 描述的。

    【讨论】:

      【解决方案6】:
      NSDictionary *dict = [list objectAtIndex: 0];
      
      NSMutableArray *vehicleGroups = [dict objectForKey:@"identity"];
      

      在我看来,dict 是一个包含三个条目的字典:资产、身份和系统 ID。在这个级别,identity 的值只是一个字符串“DEMO”,但您正试图将它分配给一个可变数组。我想你想要这样的东西:

      NSDictionary *dict = [list objectAtIndex: 0];
      NSArray *assets = [dict objectForKey:@"assets"];
      NSArray *vehicleGroups = [assets objectForKey:@"identity"];
      

      (可以将-objectForKey: 发送到一个数组 - 您会返回一个对象数组,该数组对应于接收器中每个对象的给定键。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-22
        • 2018-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多