2016-09-01 49 views
0
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' 

結果第一項爲無效

enter image description here

它看起來像出口:

{ 
    "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()); 
      } 
     }); 

=>值是:[NULL,{名稱=總站柏林,城市=柏林},{名稱= Hackerscher廣場,城市=柏林},{名稱=總站法蘭克福, 城市=法蘭克福} ]

回答

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' 

解決了該問題。