简体   繁体   English

如何在iOS中解析嵌套字典JSON数据?

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

i am newbie in iOS Development. 我是iOS开发的新手。 i want to parse my this JSON Data into to array First array Contain all Data and Second array Contain only -demopage: array Value. 我想将我的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"
   }
   ]
  }
  ]

Here my main Dictionary Key is data i want data keey value in my One array and demopage key value in to another array here my two Array is self.imageArray and self.imagesa here my code For that 在这里,我的主要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);
    }

}

then i get my data key value and it is ok but i got only last index means here my -data key Contain three -demopage key and i get only last -demopage key value i want all -demopage key value in to self.imagesa please give me solution for that. 然后我得到我的数据键值,可以,但是我只有最后一个索引意味着这里-data键包含三个-demopage键,而我只有最后一个-demopage键值,我想将所有-demopage键值都输入到self.imagesa中给我解决方案。 also my Webservices link is Link 我的Web服务链接也是Link

You can give it a try like this: 您可以这样尝试:

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

Then you got imageArray as dataSource for the collectionView . 然后,将imageArray作为collectionView

First get -data in NSMutableArray . 首先在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];

I would use SBJson library, but not the last 4th version: 我将使用SBJson库,但不使用最后的第4版:

pod 'SBJson', '3.2'

Sample code, I changed variable names and formatting a little bit: 示例代码,我更改了变量名称并进行了一些格式化:

// 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)
    });
}

And when you set a cell, you can use something like this: 而且,当您设置一个单元格时,可以使用如下所示的内容:

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