我有以下JSON文件
{
"TestNode" : {
"TestKey" : "TestValue",
"TestKeyTwo" : "TestValueTwo"
},
"TestNode2" : {
"One" : {
"ItemOne" : "email@gmail.com",
"ItemTwo" : "zYMnPqefn7BlGDidpOocff6Xcg==\nBKb1sfaDdgfe0=\n1yVcV0mciIQXtAf/4LqGdDw==\n",
"ItemThree" : "name"
},
"Two" : {
"email" : "emailtwo@gmail.com",
"password" : "xFBvDPcnp/efebUhtefZ0+XMFfw==\ngAb5fSbzAefeQXc=\ng/WdCpusQl2umtPVOefefzjs1A==\n",
"username" : "othername"
}
}
}
听众代码
TestNode.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.hasChild(TestKey)) {
String TestKeyString = TestNode.child(TestKey).getKey();
String TestValueString = TestNode.child(TestKey).getValue();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
我收到了错误
无法解析方法getValue();
P.S我还没有运行代码,因为上面的错误
答案 0 :(得分:0)
试试这段代码:
TestNode.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("TEST","key: "+dataSnapshot.getKey());
Log.d("TEST","value: "+dataSnapshot.getValue());
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
<强>更新强>
尝试此代码只能获得一个孩子
TestNode.orderByKey().equalTo(TestKey).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("TEST","key: "+dataSnapshot.getKey());
Log.d("TEST","value: "+dataSnapshot.getValue());
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
答案 1 :(得分:0)
这是您的更新代码,这肯定会有效:
TestNode.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.hasChild(TestKey)) {
String TestValueString = dataSnapshot.child(TestKey).getValue(String.class);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
希望这能解决问题。 将为您提供该密钥本身的具体价值。