如何从Firebase数据中获取特定的键值? [英] How to get particular key value from firebase data?
本文介绍了如何从Firebase数据中获取特定的键值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
{
"DIV_1" : {
"ACTINACT" : 1,
"COORDINATOR" : "10CA056",
"DIV_CODE" : "BSL",
"DIV_ID" : 1,
"DIV_NAME" : "Bhusawal",
"ERP_LOC_CODE" : "CRB",
"MTIME" : "2017-04-08T11:02:59",
"ZONE_ID" : "ZONE_1"
},
"DIV_10" : {
"ACTINACT" : 1,
"COORDINATOR" : "06CS011",
"DIV_CODE" : "UMB",
"DIV_ID" : 10,
"DIV_NAME" : "Ambala",
"ERP_LOC_CODE" : "NRA",
"MTIME" : "2017-04-08T11:02:59",
"ZONE_ID" : "ZONE_3"
}
}
如何获取DIV_ID? 最初,我需要比较键(例如DIV_10),然后需要获得DIV_NAME. 预先感谢...
How to get DIV_ID? Initially i need to compare key(Ex::Here DIV_10) and then DIV_NAME need to be get. Thanks in advance...
推荐答案
我假设DIV_1的父级,而DIV_10是根级.如果您具有DIV_ID
的值,例如"1"或"10",并且需要获取DIV_NAME
的值,那么您应该这样做:
I assume parent of DIV_1 and DIV_10 is root. If you have value of DIV_ID
like "1" or "10" and need to get value of DIV_NAME
, then you should do it like this:
int divId = 1; // sample
FirebaseDatabase.getInstance().getReference().orderByChild("DIV_ID").equalTo(divId)
.addValueEventListener(new ValueEventListener() {
... onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String divName = snapshot.child("DIV_NAME").getValue(String.class);
// there you go
// and please check if you have more than 1 value as result
}
}
...
}
但是,如果您具有像DIV_1
或DIV_10
这样的键值,那么它应该会容易得多.像这样:
But if you have key value like DIV_1
or DIV_10
then it should be lot easier. Like this:
String key = "DIV_1";
FirebaseDatabase.getInstance().getReference(key)
.addValueEventListener(new ValueEventListener() {
... onDataChange(DataSnapshot dataSnapshot) {
String divName = snapshot.child("DIV_NAME").getValue(String.class);
// note that in this sample, it doesn't need to loop, because:
// data you get here is one level deeper than data you got on first sample
}
...
}
希望这会有所帮助
这篇关于如何从Firebase数据中获取特定的键值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文