json数组的iphone顺序 [英] iphone order of json array

查看:130
本文介绍了json数组的iphone顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iPhone应用程序,该应用程序从服务器获取json字符串并进行解析.它包含一些数据,例如学校时间表信息.数据存储在nsdictionary中,然后传递到nsmuttablearray中.例如

I have an iPhone application which gets a json string from a server and parses it. It contains some data e.g. school timetable information. The data is stored in an nsdictionary and then passed into a nsmuttablearray. e.g.

有人可以指出正确的方向/告诉我如何访问数组的不同根"吗?我想将具有天"字段的元素存储为星期一"(或者也许您可以告诉我如何对它们进行排序/搜索).花了很多时间试图找到有关此信息(如何访问/更改嵌套值),但是没有运气=/.谢谢.

Could someone please point me in the right direction/tell me how i can access the different 'roots' of the array? I want to store the elements that have the field 'Day' as "Monday" (or maybe u can tell me how i can sort/do a search though them). Been spending quite some time trying to find some information on this (how to access/alter nested values) but no luck =/. thanks..

推荐答案

使用Day = Monday来访问所有Dictionary对象的最佳选择是使用带有关键路径的谓词,以了解所要查找的内容...

Your best bet for accessing all of the Dictionary objects with Day = Monday would be to use a Predicate with a Key Path to what you're after...

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"Day == 'Monday'"];
NSArray* mondayCourses = [rows filteredArrayUsingPredicate:predicate];

您也可以使谓词动态化.

You can make the predicate dynamic too.

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"Day == %@", dayOfTheWeek];

其中dayOfTheWeek可能是一个NSString,它以循环等方式保存星期几.

where dayOfTheWeek might be an NSString that holds the current day of the week in a loop etc.

如果要对数组进行排序,则可以使用排序描述符"来完成.

If you want to sort the array, you can do so using Sort Descriptors.

NSSortDescriptor* idSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"id" 
                                                                   ascending:YES];
NSSortDescriptor* levelSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"Level" 
                                                                      ascending:YES];
NSArray* sortedMondayRows = [mondayRows sortedArrayUsingDescriptors:[NSArray arrayWithObjects:idSortDescriptor, levelSortDescriptor, nil]];

这篇关于json数组的iphone顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆