英文:
Retrofit + Gson can not parse simple Map
问题
I have a JSON as follows
{
"1.2.3.4": "domain1.com",
"1.2.3.5": "domain2.com"
}
And a model
public class DnsLookup {
@Getter @Setter private Map<String, String> entry;
}
And retrofit query interface
@GET("/dns/")
Call<DnsLookup> getDns(@QueryMap Map<String, String> params);
When I make the query, I get null
in dns
.
Call<DnsLookup> call = service.getDns(queryMap);
Response<DnsLookup> response;
response = call.execute();
var dns = response.body();
Actually the original JSON is
{
"1.2.3.4": [
"domain1.com", "domain1-1.com"
],
"1.2.3.5": [
"domain2.com"
]
}
And I have tried the following model to only get null
. Since the following didn't work, I broke the problem to just key-value pairs. To my surprise, the simple map also didn't work.
public class DnsLookup {
@Getter @Setter private Map<String, List<String>> entry;
}
Am I doing a noob mistake?
英文:
I have a JSON as follows
{
"1.2.3.4": "domain1.com",
"1.2.3.5": "domain2.com"
}
And a model
public class DnsLookup {
@Getter @Setter private Map<String, String> entry;
}
And retrofit query interface
@GET("/dns/")
Call<DnsLookup> getDns(@QueryMap Map<String, String> params);
When I make the query, I get null
in dns
.
Call<DnsLookup> call = service.getDns(queryMap);
Response<DnsLookup> response;
response = call.execute();
var dns = response.body();
Actually the original JSON is
{
"1.2.3.4": [
"domain1.com", "domain1-1.com"
],
"1.2.3.5": [
"domain2.com"
]
}
And I have tried the following model to only get null
. Since the following didn't work, I broke the problem to just key-value pairs. To my surprise, the simple map also didn't work.
public class DnsLookup {
@Getter @Setter private Map<String, List<String>> entry;
}
Am I doing a noob mistake?
答案1
得分: 1
Sure, here's the translated code:
使用:
@GET("/dns/")
Call<Map<String, List<String>>> getDns(@QueryMap Map<String, String> params);
替代:
@GET("/dns/")
Call<DnsLookup> getDns(@QueryMap Map<String, String> params);
英文:
Use:
@GET("/dns/")
Call<Map<String, List<String>>> getDns(@QueryMap Map<String, String> params);
Instead of:
@GET("/dns/")
Call<DnsLookup> getDns(@QueryMap Map<String, String> params);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论