簡體   English   中英

如何在iOS中解析嵌套字典JSON數據?

[英]How to Parse Nested Dictionary JSON Data in iOS?

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

在這里,我的主要Dictionary Key是數據,我希望在我的一個數組中有數據基值,而將降級鍵值在另一個數組中,這里我的兩個Array是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鍵值都輸入到self.imagesa中給我解決方案。 我的Web服務鏈接也是Link

您可以這樣嘗試:

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

首先在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];

我將使用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"];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM