我有一个数组,我想将该数组拆分为3个部分或3个数组。
第一个数组包含 - > AppName的
第二个数组包含 - >说明
第3个数组包含 - >图标
这是我要拆分的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:@""];
我不明白这里出了什么问题。有没有解决这个问题的指针?
答案 0 :(得分: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"];
如果您的Json中有多个Deviceinfo
字典,那么您可以使用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"];
// }
答案 1 :(得分: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);
答案 2 :(得分: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"];
}