这是Web服务中json响应的一部分。
- Books :[
-{
RfNo:"1",
SegOrd:"1",
LegNo:"1",
Title:"wild elephants",
Author:"Dr.Vijitha Perera",
Country:"srilanka",
Price:"$8.99"
},
-{
RfNo:"1",
SegOrd:"2",
LegNo:"1",
Title:"butterfly guide",
Author:"Dr.Graham fox",
Country:"united kingdom",
Price:"$27.99"
},
-{
RfNo:"1",
SegOrd:"3",
LegNo:"2",
Title:"Beautiful birds",
Author:"prof.saranath kotagama",
Country:"India",
Price:"$13.99"
},
-{
RfNo:"1",
SegOrd:"2",
LegNo:"1",
Title:"Nature life",
Author:"prof.adam jules",
Country:"USA",
Price:"$22.99"
},
-{
RfNo:"2",
SegOrd:"1",
LegNo:"1",
Title:"Travel in japan",
Author:"Mr.yon hoan",
Country:"japan",
Price:"$5.99"
},
-{
RfNo:"2",
SegOrd:"2",
LegNo:"1",
Title:"Journey To New York",
Author:"Mr.Ben carlos",
Country:"USA",
Price:"$19.99"
},
-{
RfNo:"2",
SegOrd:"3",
LegNo:"2",
Title:"Life of Canada",
Author:"Dr.aron parker",
Country:"Canada",
Price:"$11.99"
},
我可以成功地将这些数据保存到
NSMutableArray
,但是现在我想要的是将所有数据放入RfNo:"1"
的araa中,而无需其他数据。这意味着我不希望RfNo:"2"
使用只有RfNo:"1"
。我该怎么做。我从响应中将这个Books
放入了这样的arrat中。NSDictionary *allResults = (NSDictionary *)resopnonse;//there isn't only the `Books` that is why I used Dictionary.
NSArray *books = [allResults objectForKey:@"Books"];
for(NSDictionary *all in books)
{
//so I want only the data which it's `RefNO:"1"`
}
我该怎么办。希望您的帮助。
最佳答案
只需为此使用NSPredicate。
NSArray *bookArray = allResults[@"Books"];
if (!bookArray) return;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"RfNo == 1"];
NSArray *filteredArray = [bookArray filteredArrayUsingPredicate:predicate];
这将为您提供RfNo = 1的书籍数组
第一个对象:
filteredArray[0]
最后一个对象:
[filteredArray lastObject]