繁体   English   中英

如何使用ios,目标C获取与json中的对象具有相同参考编号的所有值

[英]How to get the all values which have same reference number as an object in json with ios, objective C

这是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"RfNo:"1" ,而没有其他数据。这意味着我不想要RfNo:"2" ,所有仅具有RfNo:"1"数据。我该怎么做。我从响应中将这Books带入了类似的文章。

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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM