从 json ios 检索数据
Posted
技术标签:
【中文标题】从 json ios 检索数据【英文标题】:Retrieve a data from json ios 【发布时间】:2016-06-13 10:24:17 【问题描述】:这是字典*data的内容
Data =
Specialities = (
ApprovalStatus = Unapproved;
CurrencyCode = "<null>";
Packages = "<null>";
Photos = "<null>";
ServiceDescription = 356563;
Speciality = 22;
SpecialityName = "Wedding Photographer";
UserFRPs = "<null>";
Videos = (
VideoId = TWhSjpsGvPQ;
VideoLink = "http://www.youtube.com/watch?v=TWhSjpsGvPQ";
VideoType = YouTube;
,
VideoId = N2CJrhHEydA;
VideoLink = "http://www.youtube.com/watch?v=N2CJrhHEydA";
VideoType = YouTube;
,
VideoId = Lq6faQVYcwY;
VideoLink = "http://www.youtube.com/watch?v=Lq6faQVYcwY";
VideoType = YouTube;
,
VideoId = v8WjMiodcKo;
VideoLink = "http://www.youtube.com/watch?v=v8WjMiodcKo";
VideoType = YouTube;
);
,
ApprovalStatus = Unapproved;
CurrencyCode = "<null>";
Packages = "<null>";
Photos = "<null>";
ServiceDescription = 52454353;
Speciality = 37;
SpecialityName = "Hair and Makeup Stylist";
UserFRPs = "<null>";
Videos = "<null>";
,
ApprovalStatus = Unapproved;
CurrencyCode = "<null>";
Packages = "<null>";
Photos = "<null>";
ServiceDescription = 21212;
Speciality = 55;
SpecialityName = Transport;
UserFRPs = "<null>";
Videos = "<null>";
);
;
这是我从 json 得到的响应,我应该编写什么逻辑来检索链接到特定“Speciality”的“VideoId”(例如,Specialty = 22)
如果我编写以下代码,我会得到所有 @"VideoId"
[[[[responseDictionary valueForKey:@"Data"]valueForKey:@"Specialities"]valueForKey:@"Videos"]valueForKey:@"VideoId"]
【问题讨论】:
Specialities 是一个数组,每个索引都包含一个字典。 你可以使用 ObjectAtIndex 作为数组。 【参考方案1】:解析方式如下:
NSDictionary *dataDict = [responseDictionary valueForKey:@"Data"];
NSMutableArray *specialityArray = [dataDict valueForKey:@"Specialities"];
NSDictionary *objectDict = [specialityArray objectAtIndex:your index (0/1/2)];
int *speciality = [[objectDict ValueForKey:@"Speciality"] intValue];
if(speciality = 22)
NSMutableArray *videoArray = [objectDict valueForKey:@"Videos"];
NSDictionary *videoObject = [videoArray objectAtIndex:your index (0/1/2)];
NSString *videoID = [videoObject valueForKey:@"VideoId"];
根据您的需要相应地解析其余部分。
【讨论】:
感谢 Saad 的帮助,您的代码解决了我的问题,但唯一的问题是我无法对索引进行硬编码。 NSDictionary *objectDict = [specialityArray objectAtIndex:your index (0/1/2)]; 你真的想要硬编码吗?如果我们完全确定某个值及其发生,我们通常会进行硬编码。这也是一种不好的做法。 我不想硬编码 bcoz 专业 id 可能会改变,那么如何获取特定专业 id 的索引 为此,您可以遍历您的专长,如下所示:for(NSDictionary * objectDict in specialityArray) int *speciality = [[objectDict ValueForKey:@"Speciality"] intValue]; and so on.
【参考方案2】:
使用此代码,
NSArray *wholeArray = [[responseDictionary objectForKey:@"Data"] valueForKey:@"Specialities"];
NSArray *subArray = [[wholeArray objectAtIndex:0] valueForKey:@"Videos"];
subArray 是获取所有视频值,然后使用此代码获取单个值,
NSString *videoID = [[subArray objectAtIndex:i] valueForKey:@"VideoId"];
或者您将在单个变量中获取所有 videoID,请使用以下代码,
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
for (int i = 0; i < subArray.count; i++)
[tempArray addObject:[[subArray objectAtIndex:i] valueForKey:@"VideoId"]];
最终 tempArray 得到所有的 VideoID 值,在你使用的任何地方,
NSString *videoID = [tempArray objectAtIndex:0/1/2];
希望对你有帮助
【讨论】:
感谢 Iyyappan 的帮助,但我无法对索引进行编码以上是关于从 json ios 检索数据的主要内容,如果未能解决你的问题,请参考以下文章