我从“WebServices”获取“JSON数据”并将它们设置在NSMutableArray
中并尝试在页面中显示,我根据“JSON数据”使用“4个键”但是我不知道如何使用“键值”从 NSMutableArray
获取特定数据,我的“JSON`数据”是这样的 -
更多
3/5,641
JSON 数组格式 收件箱 X 阿努吉贾
晚上 9:13(14 小时前)
对我来说,Gaurav,RAjeev
<__NSArrayM 0x7f9e42556c10>(
{
ansDesc = "Researching a lead that I previously submitted ";
ansId = 1;
qsDesc = "What is your primary goal for visiting the portal, today? ";
qsId = 1;
},
{
ansDesc = "Submitting a new lead ";
ansId = 2;
qsDesc = "What is your primary goal for visiting the portal, today? ";
qsId = 1;
},
{
ansDesc = "Working on a draft lead ";
ansId = 3;
qsDesc = "What is your primary goal for visiting the portal, today? ";
qsId = 1;
},
{
ansDesc = "Daily ";
ansId = 4;
qsDesc = "How often do you use the Target Revenue Portal? ";
qsId = 2;
},
{
ansDesc = "Several times a week ";
ansId = 5;
qsDesc = "How often do you use the Target Revenue Portal? ";
qsId = 2;
},
{
ansDesc = "Once a week ";
ansId = 6;
qsDesc = "How often do you use the Target Revenue Portal? ";
qsId = 2;
},
{
ansDesc = "Every couple of weeks ";
ansId = 7;
qsDesc = "How often do you use the Target Revenue Portal? ";
qsId = 2;
},
{
ansDesc = "About once a month ";
ansId = 8;
qsDesc = "How often do you use the Target Revenue Portal? ";
qsId = 2;
},
我必须在页面中一起显示“qsId 1”和“ansId 1, 2, 3”的一组问题和相应答案,然后类似地显示“qsId 2”和“ansId 4,5,6,7” ,8”在下一页。请帮我找出最可行的方法。
谢谢
最佳答案
在 Objective C 中,NSMutableArray
不支持键值概念。 NSDictionary
确实支持它。
我认为更好的方法是使用模型类来保存四个属性并使用 NSPredicate
将它们过滤掉。
@interface JSON_Info : NSObject
//id's
@property (nonatomic, assign) int qsId;
@property (nonatomic, assign) int ansId;
//descriptions
@property (nonatomic, strong) NSString *qsDesc;
@property (nonatomic, strong) NSString *ansDesc;
@end
如果要过滤,请使用NSPredicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"qsId == 1 && ansId == 2"];
NSArray *filteredCollection = [self.JSONInfoCollection filteredArrayUsingPredicate: predicate];
关于ios - 在 Objective C 中处理 NSMutableArray 中的集合数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28164383/