2014-11-05 171 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"];