英文:
get Observable<HashMap> from Server with GSON and Retrofit
问题
以下是翻译好的内容:
我有这样的 JSON
:
我认为它在对象中是 HashMap<>
。
我正在使用 RxJava
获取 API 服务
。
服务器响应 如下:
注意:SIGNAL_URL 是动态的,作为 KEY!
我尝试了这个:
@GET("apilink")
Observable<HashMap<String, String>> getLinks();
我还有一个实体模型:
public class UsefulLinksEntity implements BaseEntity {
@SerializedName("additionalProp1")
private String additionalProp1;
@SerializedName("additionalProp3")
private String additionalProp3;
@SerializedName("additionalProp2")
private String additionalProp2;
以及 ContentModel:
public class SajamContentEntity implements BaseEntity {
@SerializedName("usefulLinks")
private UsefulLinksEntity usefulLinksEntity;
但我不知道应该如何处理这种类型的 JSON。谢谢。
英文:
I have JSON
like this :
I think it is HashMap<>
in Object.
I am using RxJava
to get API service
.
The Server Response is like this :
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.
答案1
得分: 1
以下是翻译好的内容:
请求应该是:
@GET("apilink")
Observable<ApiLink> getLinks();
ApiLink
模型:
public class ApiLink implements BaseEntity {
private int code;
private String message;
private Content content;
}
Content
模型:
public class Content implements BaseEntity {
private SajamContentEntity content;
}
将您的 SajamContentEntity
模型修改如下:
public class SajamContentEntity implements BaseEntity {
@SerializedName("usefulLinks")
private HashMap<String, String> usefulLinksEntity;
}
英文:
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;
}
答案2
得分: 1
你始终可以将其作为以字符串为键、对象为值的哈希映射(Hash map)获取,在此之后遍历键并检查 usefulLinks 键,获取其中的所有动态键和值。
@GET("apilink")
Observable<HashMap<String, Object>> getLinks();
在遍历 `getLinks` 哈希映射时,请检查键 `usefulLinks`,它将再次是一个 `HashMap<String, String>`。此时,您可以通过转换从哈希映射中获取值。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论