curl -X PUT -d '{ "1": { "name": "Hauptbahnhof Berlin", "city": "Berlin"}, "2": { "name": "Hackerscher Markt", "city": "Berlin"}, "3": { "name": "Hauptbahnhof Frankfurt", "city": "Frankfurt"} }' 'https://xxx.firebaseio.com/stations.json'
结果
看起来像导出:
{
"stations" : [ null, {
"city" : "Berlin",
"name" : "Hauptbahnhof Berlin"
}, {
"city" : "Berlin",
"name" : "Hackerscher Markt"
}, {
"city" : "Frankfurt",
"name" : "Hauptbahnhof Frankfurt"
} ]
}
现在选择数据的值为空,为什么会这样呢?
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference stationsRef = database.getReference("stations");
stationsRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<Map<String, String>> value = (ArrayList<Map<String, String>>) dataSnapshot.getValue();
Log.d(TAG, "Value is: " + value.toString());
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
}
});
=&GT;价值是:[null,{name = Hauptbahnhof Berlin,city = Berlin},{name = Hackerscher Markt,city = Berlin},{name = Hauptbahnhof Frankfurt, 城市=法兰克福}]
答案 0 :(得分:1)
如果未传递,Firebase会自动将索引0添加为空值。所以将插入更改为
curl -X PUT -d '{ "0": { "name": "Hauptbahnhof Berlin", "city": "Berlin"}, "1": { "name": "Hackerscher Markt", "city": "Berlin"}, "2": { "name": "Hauptbahnhof Frankfurt", "city": "Frankfurt"} }' 'https://xxx.firebaseio.com/stations.json'
解决了这个问题。