【发布时间】:2016-09-23 13:18:10
【问题描述】:
如何使用查询按值获取给定的 json 对象。详细来说,想得到1 for id:4 不循环json表中的所有事件
【问题讨论】:
-
您使用的是哪种语言? Obj-C 还是 Swift?你的标签有点混乱......
标签: ios objective-c swift firebase firebase-realtime-database
如何使用查询按值获取给定的 json 对象。详细来说,想得到1 for id:4 不循环json表中的所有事件
【问题讨论】:
标签: ios objective-c swift firebase firebase-realtime-database
斯威夫特 2
FIRDatabase.database().reference().child("events").queryOrderedByChild("id").queryEqualToValue(4).observeSingleEventOfType(.Value, withBlock: {(Snap) in
if let snapDict = Snap.value as? [String:AnyObject]{
for each in snapDict{
print(each.0) // Will print out your node ID = 1
print(each.1) //Will print out your Dictionary inside that node.
}
}
})
斯威夫特 3
FIRDatabase.database().reference().child("events").queryOrdered(byChild: "id").queryEqual(toValue: 4).observeSingleEvent(of: .value, with: {(Snap) in
if let snapDict = Snap.value as? [String:AnyObject]{
for each in snapDict{
print(each.0) // Will print out your node ID = 1
print(each.1) //Will print out your Dictionary inside that node.
}
}
})
【讨论】:
或许能解决你的问题
[self.dbRef observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
if (![snapshot.value isKindOfClass:[NSNull class]]) {
NSArray *arrResponse = (NSArray *)snapshot.value;
NSPredicate *pred = [NSPredicate predicateWithFormat:@"id = %@", serchID];
NSArray *arrSearchedVal = [arrResponse filteredArrayUsingPredicate:pred];
}
}];
注意: dbRef 是您的 FIRDatabaseReference,它返回整个数据 JSON。然后将 NSPredicate 与您的搜索 id 一起使用,您将获得结果。
编码愉快..
【讨论】: