ios - 如何使用ios, objective-c 获取与json中的对象具有相同引用号的所有值

标签 ios objective-c json

这是我在网络服务中的 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/

相关文章:

iphone - 这个方法背后的代码在哪里

android - Mac OS 上的 Phonegap eclipse 环境

ios - Xcode 4.5 + UIScrollView : Cannot see struts and springs (OSX 10. 8 山狮)

json - 如何使用 Pig 读取非分隔的 JSON?

iOS7 Autolayout 和 UIImageview 过大

ios - 当触地按钮( subview )时,不要处理 super View 上的平移手势

iphone - 泛化对 NSLog 的调用

objective-c - xcode4 和助理编辑器中非常基本的 socket / Action 连接问题

iphone - 应用未运行时检查 JSON 内容的变化

ruby-on-rails - 我应该使用 `respond_to` 还是在 Rails 中有一个专用的 API 命名空间?