Sana Ebadi
Sana Ebadi

Reputation: 7220

get Observable<HashMap> from Server with GSON and Retrofit

I have JSON like this :

enter image description here

I think it is HashMap<> in Object.

I am using RxJava to get API service.

The Server Response is like this :

enter image description here

NOTE: The SIGNAL_URL IS dynamic, as KEY!

I trided this :

 @GET("apilink")
    Observable<HashMap<String, String>> getLinks();

I have an Entity Model too:

public class UsefulLinksEntity implements BaseEntity {

    @SerializedName("additionalProp1")
    private String additionalProp1;

    @SerializedName("additionalProp3")
    private String additionalProp3;

    @SerializedName("additionalProp2")
    private String additionalProp2;

and ContentModel :

public class SajamContentEntity implements BaseEntity {

@SerializedName("usefulLinks")
private UsefulLinksEntity usefulLinksEntity;

but I do not know how should work with this kind of JSON. Thank you.

Upvotes: 1

Views: 308

Answers (2)

GianhTran
GianhTran

Reputation: 3711

the request should be

@GET("apilink")
    Observable<ApiLink> getLinks();

ApiLink model:

public class ApiLink implements BaseEntity {

   private int code;

   private String message;

   private Content content;
}

Content model:

public class Content implements BaseEntity {  
   private SajamContentEntity content;
}

modify your SajamContentEntity model as below

public class SajamContentEntity implements BaseEntity {

     @SerializedName("usefulLinks")
     private HashMap<String, String> usefulLinksEntity;
}

Upvotes: 1

vikas kumar
vikas kumar

Reputation: 11028

you can always get it as Hash map of String as key and Object as value after that traverse the key and check for usefulLinks key and get all the dynamic keys inside it and value.

@GET("apilink")
Observable<HashMap<String, Object>> getLinks();

when you traverse the getLinks hashmap check for key usefulLinks which will be again a of HashMap<String, String>. at this point you can get the value from hashmap by casting.

Upvotes: 1

Related Questions