2017-10-17 34 views
0

Les classes Can Gson contiennent-elles des champs de type JSONObject? Voici ma classe GSONChamps JSONObject dans la classe GSON - Reste vide

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? = "" 
} 

Les champs JSONObject segments et global reste un JSONObject vide lorsque sérialisé (en utilisant par défaut Retrofit GsonConverterFactory). voici ce que j'ai. Des suggestions pour l'écrire?

{"items":[{"agr":{},"name":"Logged In","seg":{},"ts":"2017-10-17T12:20:32Z","type":"event"}]} 
+0

vous avez besoin d'un désérialiseur personnalisé, vous pouvez trouver plus d'informations ici: https://stackoverflow.com/questions/16590377/custom-json-deserializer-using-gson – Jibbo

Répondre

0

Un travail autour - Remplacement du JSONObject avec un HashMap() avait m'a aidé à atteindre les mêmes résultats.

0

1.You peut simplement réécrire cette classe, dans l'espoir de vous aider:

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

}