这是我在网络服务中的 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
。但现在我想要的是将所有数据放入一个 arrya,它是 RfNo:"1"
,无需others.that means I don't want RfNo:"2"
, all data with only 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]
关于ios - 如何使用ios, objective-c 获取与json中的对象具有相同引用号的所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35116981/