0
json을 생성 된 클래스로 구문 분석하려고하지만이 클래스의 필드에서 null을받습니다.개조 안드로이드로 json 데이터 구문 분석
JSON은 다음과 같습니다
Call<WorkingMonth> callPings = HelperClass.getService().getPing(month, year);
callPings.enqueue(new Callback<WorkingMonth>() {
@Override
public void onResponse(Call<WorkingMonth> call, Response<WorkingMonth> response) {
if (response.isSuccessful()) {
WorkingMonth wm = response.body();
wm.getWorkingDays();
} else {
Log.i(TAG, "error in downloading");
}
}
@Override
public void onFailure(Call<WorkingMonth> call, Throwable t) {
Log.i(TAG, t.toString());
}
});
클래스 WorkingMonth :
public class WorkingMonth{
private Map<String,List<Ping>> workingDays;
public Map<String,List<Ping>> getWorkingDays() {
return workingDays;
}
public void setWorkingDays(Map<String,List<Ping>> workingDays) {
this.workingDays = workingDays;
}
클래스 핑 :
public class Ping {
private Long id;
private String dateIn;
private String dateOut;
//getters, setters
}
,536,913,632
다음
{"13": [
{ "id": 3654, "dateIn": "2017-02-13 13:13:13", "dateOut": "2017-02-13 15:13:13" },
{ "id": 3656, "dateIn": "2017-02-13 17:13:13", "dateOut": "2017-02-13 17:13:13" },
{ "id": 3655, "dateIn": "2017-02-13 16:13:13", "dateOut": "2017-02-13 17:13:13" }
],
"14": [
{ "id": 3654, "dateIn": "2017-02-13 13:13:13", "dateOut": "2017-02-13 15:13:13" },
{ "id": 3656, "dateIn": "2017-02-13 17:13:13", "dateOut": "2017-02-13 17:13:13" },
{ "id": 3655, "dateIn": "2017-02-13 16:13:13", "dateOut": "2017-02-13 17:13:13" }
]
}
이 개조의 방법입니다 10
WorkingMonth 클래스의 개체를 받지만 workingDays 필드는 null입니다. 도움을 많이 주시면 감사하겠습니다. 감사! 나는 해결책을 발견
UPDATE. 나는
private class HolderDeserializer implements JsonDeserializer<WorkingMonth> {
@Override
public WorkingMonth deserialize(JsonElement json, Type type, JsonDeserializationContext context)
throws JsonParseException {
Type mapType = new TypeToken<Map<String, List<Ping>>>() {}.getType();
Map<String, List<Ping>> data = context.deserialize(json, mapType);
return new WorkingMonth(data);
}
}
디시리얼라이저 내 사용자 지정지도를 작성했다 다음
HolderDeserializer holderDeserializer = new HolderDeserializer();
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(WorkingMonth.class, holderDeserializer);
Gson gson = gsonBuilder.create();
Type responseType = new TypeToken<WorkingMonth>() {}.getType();
WorkingMonth response = gson.fromJson(json, responseType);
어떤 컨버터를 사용하고 있습니까? 기본적으로 내장 컨버터 중 어느 것도 키와 값을 HashMap에 매핑하지 않습니다. – akash93
명시 적으로'GsonBuilder.excludeFieldsWithoutExposeAnnotation()'을 설정하지 않는 한 Btw @Blackbelt'@ Expose'는 무의미합니다. – akash93
@ akash93 gson을 사용합니다. 이미 해결책을 찾았습니다. 내 자신의지도 디시리얼라이저를 작성해야했습니다. 질문은 솔루션으로 업데이트됩니다. 감사합니다 – mesomagik