2017-12-20 6 views
3

Die Ergebnis Variable enthält korrigierten geparsten JSON. Aber nach DeserialisierungList enthält die richtige Anzahl von Elementen, aber alle sind leer. Wie es zu beheben?Gson deserialisiert mit leeren Feldern

Gson gson = new Gson(); 
List<UnitView> unitViews = new ArrayList<UnitView>(); 
// https://stackoverflow.com/questions/5554217/google-gson-deserialize-listclass-object-generic-type 
Type typeToken = new TypeToken<List<UnitView>>() { }.getType(); 
unitViews = gson.fromJson(result,typeToken); 

Auch wenn ich wie

UnitView[] unitViews = gson.fromJson(result, UnitView[].class); 

Die Felder der Elemente sind ebenfalls leer.

UnitView

public class UnitView implements Serializable { 

    public String id ; 

    public String name ; 

    public String description ; 

    public String deviceTypeName ; 


    public String categoryID ; 

    public String lastOnline ; 

    public String latitude ; 

    public String longitude ; 

    public String atTime ; 

    public String getId() { 
     return id; 
    } 

    public String getName() { 
     return name; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public String getDeviceTypeName() { 
     return deviceTypeName; 
    } 

    public String getCategoryID() { 
     return categoryID; 
    } 

    public String getLastOnline() { 
     return lastOnline; 
    } 

    public String getLatitude() { 
     return latitude; 
    } 

    public String getLongitude() { 
     return longitude; 
    } 

    public String getAtTime() { 
     return atTime; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

    public void setDeviceTypeName(String deviceTypeName) { 
     this.deviceTypeName = deviceTypeName; 
    } 

    public void setCategoryID(String categoryID) { 
     this.categoryID = categoryID; 
    } 

    public void setLastOnline(String lastOnline) { 
     this.lastOnline = lastOnline; 
    } 

    public void setLatitude(String latitude) { 
     this.latitude = latitude; 
    } 

    public void setLongitude(String longitude) { 
     this.longitude = longitude; 
    } 

    public void setAtTime(String atTime) { 
     this.atTime = atTime; 
    } 
} 

JSON DATA

[{"ID":"294","Name":"Foton Tunland № F110","Description":null,"DeviceTypeName":"Техника ТО","CategoryID":"18","LastOnline":"19.12.2017 20:38:04","Latitude":"11,40119","Longitude":"11,42403","AtTime":"19.12.2017 20:38:04"},{"ID":"295","Name":"DML LP1200 № 9793","Description":null,"DeviceTypeName":"Буровой станок дизельный","CategoryID":"15","LastOnline":null,"Latitude":null,"Longitude":null,"AtTime":null}] 
+0

könnten Sie Ihre JSON-Daten in Ihre Frage eingeben? – diegoveloper

+0

wir müssen möglicherweise Ihre JSON sehen –

+0

@diegoveloper Done. Bitte überprüfen. –

Antwort

2

Ok, das Problem ist, dass der Parser case-sensitive ist, können Sie den Namen Ihrer Attribute ändern Sie den Namen übereinstimmen der Json-Wert von Ihnen könnte die SerializedName Annotation wie folgt verwenden:

@SerializedName("ID") 
public String id ; 
@SerializedName("Name") 
    public String name ; 
@SerializedName("Description") 
    public String description; 
... 

oder

public String ID ; 
    public String Name ; 
    public String Description ; 
... 
-2

Ich denke, dieses Problem in Ihrem json wegen Nullwerte hast. Überprüfen Sie es. Source

Verwandte Themen