gpt4 book ai didi

ios - 如何根据页码排列 JSON 解析数组?

转载 作者:行者123 更新时间:2023-11-28 21:54:51 25 4
gpt4 key购买 nike

我是 iOS 开发的新手,我像这样解析我的 JSON 数据

{
mid: "1",
name: "august 2014",
front_image:"http://www.truemanindiamagazine.com/webservice/magazineimage/frontimage/1.jpg",
title: "August fist edition",
release_date: "2014-08-01",
short_description: "Here we present to you for the first time our Magazine Trueman India. “Trueman India” covers Bollywood Gossips, film reviews, upcoming movies, F",
current_issue: 0,
-demopage: [
{
link:my image URL
page_no: "1"
},
{
link: my image URL,
page_no: "16"
},
{
link: my image URL,
page_no: "2"
},
{
link: my image URL,
page_no: "22"
},
{
link: my image URL,
page_no: "25"
},
{
link: my image URL,
page_no: "26"
},

然后我将这些数据解析为两个数组,如下所示

NSError* error;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
self.imagesa= nil;
if([jsonDict count]>0)
{
NSArray *arrData = [jsonDict objectForKey:@"data"];
if([arrData count]>0)
{
NSDictionary *imgDict = [arrData objectAtIndex:1];
if([imgDict count]>0)
{
NSMutableArray *imgsLinkArray = [imgDict objectForKey:@"demopage"];
self.imagesa = imgsLinkArray;
}
}
}

然后它被解析并且我得到了我想要的结果但是这里有一个问题是当我在 ScrollView 中显示带有页码的图像然后我根据我得到的 JSON 数据显示我的图像但我希望我的 ImageView 是基于在 PageNumber 上,例如从 1 到数据数组 Size。请给我解决方案,我根据我的页码安排我的图像 明智的是,我的图像和页码解析代码是

for(int index=0; index < [self.imagesa count]; index++)
{
NSDictionary *dict=[self.imagesa objectAtIndex:index];
NSString *image=[dict valueForKey:@"link"];
NSString *pageNumber=[dict valueForKey:@"page_no"];
smallImage = [[UIImageView alloc] init];
pageLabel=[[UILabel alloc]init];
[pageLabel setBackgroundColor:[UIColor clearColor]];
[smallImage sd_setImageWithURL:[NSURL URLWithString:image] placeholderImage:[UIImage imageNamed:@"1.png"]];
[pageLabel setText:[NSString stringWithFormat:@"Page%@",pageNumber]];

然后我将我的标签和 ImageView 添加到我的 ScrollView 中,然后它按我想要的方式工作,但它不是显示基于页面编号的图像,如来自!到数组大小,请给我解决方案。

最佳答案

按以下方式对您的 imagesLinkArray 进行排序:

self.imagesa = [[imgsLinkArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
if ([obj1[@"page_no"] intValue] > [obj2[@"page_no"] intValue]) {
return NSOrderedDescending;
} else if ([obj1[@"page_no"] intValue] < [obj2[@"page_no"] intValue]) {
return NSOrderedAscending;
}
return NSOrderedSame;
}] mutableCopy];

关于ios - 如何根据页码排列 JSON 解析数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26931900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com