我有一个数组,我想将该数组分成 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""];
我不明白这里出了什么问题。有解决这个问题的指点吗?
Best Answer-推荐答案 strong>
我认为您可以使用以下代码,希望对您有所帮助:-
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"];
// }
关于iphone - 将数组拆分为三个单独的数组,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/18584797/
|