我是iOS开发中的新手。我想将我的这个JSON数据解析为数组第一个数组包含所有数据和第二个数组仅包含-demopage:数组值。
status: "success",
-data: [
{
mid: "5",
name: "October 2014",
front_image: "http://www.truemanindiamagazine.com/webservice/magazineimage/frontimage/01.jpg",
title: "October 2014",
release_date: "2014-10-01",
short_description: "As the name suggest itself “Trueman India” will cover icons of India. Our national Magazine “Trueman India” is an expansion to our business, i",
current_issue: 0,
-demopage: [
{
link: "http://www.truemanindiamagazine.com/webservice/magazineimage/pageimage/2014/10/01-1413806104.jpg",
page_no: "1"
},
{
link: "http://www.truemanindiamagazine.com/webservice/magazineimage/pageimage/2014/10/2-1413806131.jpg",
page_no: "2"
},
{
link: "http://www.truemanindiamagazine.com/webservice/magazineimage/pageimage/2014/10/3-1413806170.jpg",
page_no: "3"
}
]
}
]
这里我的主要词典键是数据我希望我的One数组中的数据键值和demopage键值到另一个数组这里我的两个数组是self.imageArray和self.imagesa这里我的代码为此
- (void)viewDidLoad
{
[super viewDidLoad];
[self.collectionView registerNib:[UINib nibWithNibName:@"CustumCell" bundle:nil] forCellWithReuseIdentifier:@"CellIdentifier"];
dispatch_async(kBgQueue, ^{
data = [NSData dataWithContentsOfURL: imgURL];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});
}
-(void)fetchedData:(NSData *)responsedata
{
NSMutableArray *imagesArray = [[NSMutableArray alloc]init];
if (responsedata.length > 0)
{
NSError* error;
self.json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
if ([[_json objectForKey:@"data"] isKindOfClass:[NSArray class]])
{
NSArray *arr = (NSArray *)[_json objectForKey:@"data"];
[self.imageArray addObjectsFromArray:arr];
[self.storeTable reloadData];
}
self.storeTable.hidden=FALSE;
for (index=0; index<[self.imageArray count]; index++)
{
for(NSDictionary *dict in self.imageArray)
{
imagesArray = [dict valueForKey:@"demopage"];
self.imagesa = imagesArray;
}
NSLog(@"New Demo page array %@",self.imagesa);
}
}
然后我得到我的数据键值,它是好的,但我只有最后一个索引意味着在这里我的-data键包含三个-demopage键,我只得到最后一个-demopage键值我想要所有-demopage键值到自我.imagesa请给我解决方案。 我的Webservices链接也是Link
答案 0 :(得分:2)
你可以尝试这样:
NSError* error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
NSDictionary *dataDict = [json objectForKey:@"data"];
NSArray *imageArray = [dataDict objectForKey:@"demopage"];
self.imagesa = [NSMutableArray array];
for (NSDictionary *dict in array) {
NSString *imageUrl = [dict objectForKey:@"link"];
[self.imagesa addObject:imageUrl];
}
然后你将imageArray作为collectionView
的数据源。
答案 1 :(得分:2)
首先在 NSMutableArray 中获取-data。
NSMutableArray *dataArray = [[NSMutableArray alloc]init];
NSMutableArray *imagesArray = [[NSMutableArray alloc]init];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
dataArray=[json objectForKey:@"data"];
for(NSDictionary *dict in dataArray )
{
imagesArray = [dict valueForKey:@"demopage"];
self.imagesa = imagesArray;
}
[selt.tableView reloadData];
答案 2 :(得分:0)
我会使用SBJson
库,但不会使用最后的第4版:
pod 'SBJson', '3.2'
示例代码,我更改了变量名称和格式:
// Create a JSON String from NSData
NSData *responseData = [NSData new]; // Here you should use your responseData
NSString *jsonString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
if (! jsonString ) {
// Handle errors
}
// Create SBJsonParser instance
SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
// Parse JSON String to dictionary
NSDictionary *jsonDic = [jsonParser objectWithString:jsonString];
// Get an array by key
NSArray *dicArray = [jsonDic objectForKey:@"demopage"];
// Reload collection view
if ( [dicArray count] > 0 ) {
dispatch_async(dispatch_get_main_queue(), ^{
// Reload Your Collection View Here and use dicArray (in your case imagesa array with dictionaries)
});
}
当你设置一个单元格时,你可以使用这样的东西:
NSDictionary *dic = dicArray[indexPath.row];
NSString *link = dic[@"link"];
NSString *pageNo = dic[@"page_no"];