2014-11-05 165 views
1

我是iOS开发人员。我想解析我的这个JSON数据到数组中第一个数组包含所有数据和第二个数组仅包含-demopage:数组值。如何解析iOS中的嵌套字典JSON数据?

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" 
    } 
    ] 
    } 
    ] 

这里我主要解释主要是数据我想在这里另一个数组在我的一个阵列和demopage键值数据keey值我的两个数组是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); 
    } 

} 

然后我得到我的数据键值,它是好的,但我只有最后一个索引意味着在这里我的数据键包含三个-demopage键和我只得到最后-demopage键值我希望所有-demopage键值self.imagesa请给我解决方案。 也是我的web服务链接Link

+0

您可以添加您的完整WebServices数据 – 2014-11-05 10:23:40

回答

2

首先得到-data NSMutableArray

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

#Fawad我得到的数据,因为我想但我如何显示在集合视图单元格?我写了一个代码Collectionview Cell CustumCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@“CellIdentifier”forIndexPath:indexPath]; NSDictionary * dict = [self.imagesa objectAtIndex:indexPath.item]; NSString * img2 = [dict valueForKey:@“link”]; [cell.imageview sd_setImageWithURL:[NSURL URLWithString:img2] placeholderImage:[UIImage imageNamed:@“1.png”]]; return cell;但它不起作用 – 2014-11-05 11:10:41

+0

检查是否调用了方法cellForItemAtIndexPath。如果它调用NSLog img2字符串并告诉我它是否显示。另外检查你是否已经设置cell的委托像cell.delegate = self。 – 2014-11-05 11:29:34

+0

兄弟的方法不叫我哪个问题我不知道我很累请给我解决方案。 – 2014-11-05 11:46:24

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

+0

#gabbler在哪种方法我写代码? – 2014-11-05 10:32:21

+0

在fetchedData中,:)。 – gabbler 2014-11-05 10:33:45

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