2017-12-20 4 views
3

결과 변수에 수정 된 JSON이 수정되었습니다. 이후에 역 직렬화List에 정확한 항목이 들어 있지만 모두 은 비어 있습니다. 해결 방법?Gson이 빈 필드로 비 직렬화

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); 

내가

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

같이 할 경우에도 항목의 필드도 비어 있습니다.

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 데이터가

[{"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

질문에 json 데이터를 넣을 수 있습니까? – diegoveloper

+0

우리는 귀하의 json –

+0

@diegoveloper Done을보아야 할 수도 있습니다. Pls 그것을 확인하십시오. –

답변

2

좋아, 문제는 파서가 대소 문자를 구분, 당신은 이름과 일치하도록 속성의 이름을 변경할 수 있습니다 님의 json 값 중 SerializedName 주석을 사용할 수 있습니다.

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

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

또는

난 당신 때문에 당신의 JSON에서 null 값이 문제가 있다고 생각. 확인하십시오. Source

관련 문제