2017-10-17 1 views
0

Gson 클래스에 JSONObject 유형의 필드가 포함되어 있습니까? 다음은 집계은 (개조하여 기본 GsonConverterFactory에) 연재 빈 된 JSONObject 남아 내 GSON 클래스GSON 클래스의 JSONObject 필드 - 비어 있습니다.

class Item { 
    @Expose 
    var name: String? = "" 
    @Expose 
    var type: String? = "" 
    @Expose 
    @SerializedName("agr") 
    var aggregate: JSONObject? = null 
    @Expose 
    @SerializedName("seg") 
    var segments: JSONObject? = null 
    @Expose 
    @SerializedName("ts") 
    var timestamp: String? = "" 
} 

된 JSONObject 필드 세그먼트입니다. 여기 내가 가진거야. 그것을 얻기 위해 어떤 제안?

{"items":[{"agr":{},"name":"Logged In","seg":{},"ts":"2017-10-17T12:20:32Z","type":"event"}]} 
1.You 단순히 도움을 기대하고,이 클래스를 다시 작성할 수 있습니다
+0

맞춤 디시리얼라이저가 필요하다면 여기에서 자세한 정보를 확인할 수 있습니다. https://stackoverflow.com/questions/16590377/custom-json-deserializer-using-gson – Jibbo

답변

0

작품을 - 의 HashMap으로 된 JSONObject 교체()가 있었다 동일한 결과를 얻을 수있었습니다.

0

: 주위

public class GsonConverterFactory extends Converter.Factory { 
/** 
* Create an instance using a default {@link Gson} instance for conversion. Encoding to JSON and 
* decoding from JSON (when no charset is specified by a header) will use UTF-8. 
*/ 
public static GsonConverterFactory create() { 
    return create(new Gson()); 
} 
/** 
* Create an instance using {@code gson} for conversion. Encoding to JSON and 
* decoding from JSON (when no charset is specified by a header) will use UTF-8. 
*/ 
public static GsonConverterFactory create(Gson gson) { 
    return new GsonConverterFactory(gson); 
} 
private final Gson gson; 
private GsonConverterFactory(Gson gson) { 
    if (gson == null) throw new NullPointerException("gson == null"); 
    this.gson = gson; 
} 
@Override 
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, 
                 Retrofit retrofit) { 
    TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type)).nullSafe(); 
    return new GsonResponseBodyConverter<>(gson, adapter); 
} 
@Override 
public Converter<?, RequestBody> requestBodyConverter(Type type, 
                 Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) { 
    TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type)).nullSafe(); 
    return new GsonRequestBodyConverter<>(gson, adapter); 
} 

}

관련 문제