2

我有一个数组,我想将该数组分成 3 个部分或 3 个数组。

第一个数组包含 -> AppName
第二个数组包含 -> 描述
第三个数组包含 -> 图标

这是我要拆分的json数组,

Deviceinfo =   (
                {
            Appname = App;
            Description = "This is test app";
            Icon = "57.png";
        }
    );
}

这是我的代码,

NSMutableArray *firstArray = [NSMutableArray array];
NSMutableArray *secondArray = [NSMutableArray array];
NSMutableArray *thirdArray = [NSMutableArray array];

for (int i = 0; i < [json count]; i++) {
    NSArray *tempArray = [[json objectAtIndex:i]componentsSeparatedByString:@""];
    [firstArray addObject:[tempArray objectAtIndex:0]];
    [secondArray addObject:[tempArray objectAtIndex:1]];
    if ([tempArray count] == 3)
    {
        [thirdArray addObject:[tempArray objectAtIndex:2]];
    }
}
NSLog(@"yourArray: %@\nfirst: %@\nsecond: %@\nthird: %@", json, firstArray, secondArray, thirdArray);

我观察到这一行的代码崩溃,

NSArray *tempArray = [[json objectAtIndex:i]componentsSeparatedByString:@""];

我不明白这里出了什么问题。任何解决此问题的指针?

4

3 回答 3

1

我认为您可以使用以下代码,希望对您有所帮助:-

NSMutableArray *firstArray = [NSMutableArray array];
NSMutableArray *secondArray = [NSMutableArray array];
NSMutableArray *thirdArray = [NSMutableArray array];

NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData: jsonResponse options: NSJSONReadingMutableContainers error: &e]; 

 //here is first i load with Dicutionary bcz if into your Json you have may be multiple Dictuionary so you then you can load purticular dictionary as bellow line 

编辑

  NSArray * responseArr = jsonArray[@"Deviceinfo"];

  firstArray = [responseArr valueForKey:@"Appname"];
  secondArray = [responseArr valueForKey:@"Description"];
  thirdArray = [responseArr valueForKey:@"Icon"];

如果您Deviceinfo的 Json 中有多个字典,那么您可以使用 For 循环

 //    NSArray * responseArr = jsonArray[@"Deviceinfo"];
 //      for (NSDictionary *dict in responseArr) {

 //        [firstArray addObject:[dict valueForKey:@"Appname"];
 //        [secondArray addObject:[dict valueForKey:@"Description"];
 //       [thirdArray addObject:[dict valueForKey:@"Icon"];
 //     }
于 2013-09-03T05:40:59.757 回答
0
NSMutableArray *firstArray = [[NSMutableArray alloc]init];
NSMutableArray *secondArray = [[NSMutableArray alloc]init];
NSMutableArray *thirdArray = [[NSMutableArray alloc]init];

NSDictionary *list =[NSJSONSerialization JSONObjectWithData:data
                                                                options:kNilOptions error:&error];

 NSArray * tempArray = jsonArray[@"Deviceinfo"];
for (NSDictionary *list in tempArray)
{
  [firstArray addObject:[list objectForKey:@"Appname"];
   [secondArray addObject:[list objectForKey:@"Description"];
   [thirdArray addObject:[list objectForKey:@"Icon"];
 }

然后尝试

NSLog(@"yourArray: %@\nfirst: %@\nsecond: %@\nthird: %@", tempArray, firstArray, secondArray, thirdArray);
于 2013-09-03T05:41:38.043 回答
0
NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://url.to.your.json"]];

NSArray *jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];

NSMutableArray *appNameArray = [NSMutableArray array];
NSMutableArray *discriptionArray = [NSMutableArray array];
NSMutableArray *iconArray = [NSMutableArray array];

for(NSDictionary *dictionary in jsonObjects)
{
     [appNameArray adddObject:[dictionary valueForKey:@"Appname"];
     [appNameArray adddObject:[dictionary valueForKey:@"Description"];
     [appNameArray adddObject:[dictionary valueForKey:@"Icon"];
}
于 2013-09-03T05:43:38.243 回答