[英]How to deal with API JSON response, which is having value in JSON name field
All the while, I'm using retrofit and JSON to POJO tool , in order to have Android app talk with API server. 一直以来,我都在使用改装和JSON到POJO工具 ,以便让Android应用与API服务器通话。
The API server is using JSON as response. API服务器使用JSON作为响应。
Recently, I encounter stock related response, which looks like this 最近,我遇到了股票相关的回应,看起来像这样
{
"APPL": {
"quote": {
"symbol": "AAPL",
"lastPrice": 1.23
},
"stats": {
"dividendRate":2.52
}
},
"GOOGL": {
"quote": {
"symbol": "GOOGL",
"lastPrice": 4.56
},
"stats": {
"dividendRate":7.89
}
}
}
APPL and GOOGL , are company stock codes. APPL和GOOGL是公司的股票代码。 I view them as value
. 我认为它们是value
。 Hence, I don't expect them to be placed at JSON's name field (Left hand side) 因此,我不希望它们被放置在JSON的名称字段(左侧)
I can hardly generate a suitable set of classes for the above response. 我几乎无法为上述响应生成一组合适的类。
If I plug the JSON output directly to http://www.jsonschema2pojo.org/ , the result I'm getting are 如果我将JSON输出直接插入http://www.jsonschema2pojo.org/ ,我得到的结果是
public class BatchResponse {
@SerializedName("APPL")
@Expose
private APPL aPPL;
@SerializedName("GOOGL")
@Expose
private GOOGL gOOGL;
public APPL getAPPL() {
return aPPL;
}
public void setAPPL(APPL aPPL) {
this.aPPL = aPPL;
}
public GOOGL getGOOGL() {
return gOOGL;
}
public void setGOOGL(GOOGL gOOGL) {
this.gOOGL = gOOGL;
}
}
public class APPL {
@SerializedName("quote")
@Expose
private Quote quote;
@SerializedName("stats")
@Expose
private Stats stats;
public Quote getQuote() {
return quote;
}
public void setQuote(Quote quote) {
this.quote = quote;
}
public Stats getStats() {
return stats;
}
public void setStats(Stats stats) {
this.stats = stats;
}
}
public class GOOGL {
@SerializedName("quote")
@Expose
private Quote quote;
@SerializedName("stats")
@Expose
private Stats stats;
public Quote getQuote() {
return quote;
}
public void setQuote(Quote_ quote) {
this.quote = quote;
}
public Stats getStats() {
return stats;
}
public void setStats(Stats stats) {
this.stats = stats;
}
}
public class Quote {
@SerializedName("symbol")
@Expose
private String symbol;
@SerializedName("lastPrice")
@Expose
private double lastPrice;
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public double getLastPrice() {
return lastPrice;
}
public void setLastPrice(double lastPrice) {
this.lastPrice = lastPrice;
}
}
public class Stats {
@SerializedName("dividendRate")
@Expose
private double dividendRate;
public double getDividendRate() {
return dividendRate;
}
public void setDividendRate(double dividendRate) {
this.dividendRate = dividendRate;
}
}
Hence, if I have Tesla, Facebook, ... stocks, I have to create new classes for each stock. 因此,如果我有特斯拉,Facebook,......股票,我必须为每只股票创建新的类别。
I was wondering, how I suppose to handle the above response using retrofit ? 我想知道,我想如何使用改造处理上述响应? Is there any workaround I can do on the above API response, so that I can handle them gracefully using retrofit ? 我可以对上面的API响应做任何解决方法,以便我可以使用改造来优雅地处理它们吗?
Does it make sense, if I can propose the following response format to the API designer, so that application developer can handle such response easier? 如果我可以向API设计者提出以下响应格式,那么它是否有意义,以便应用程序开发人员可以更轻松地处理此类响应?
{
"batch" : [
{
"quote" : {
"symbol": "AAPL",
"lastPrice": 1.23
},
"stats" : {
"dividendRate":2.52
}
},
{
"quote" : {
"symbol": "GOOGL",
"lastPrice": 4.56
},
"stats" : {
"dividendRate":7.89
}
}
]
}
If using the above proposed API response, I can generate a better POJO classes. 如果使用上面提出的API响应,我可以生成更好的POJO类。
public class BatchResponse {
@SerializedName("batch")
@Expose
private List<Batch> batch = null;
public List<Batch> getBatch() {
return batch;
}
public void setBatch(List<Batch> batch) {
this.batch = batch;
}
}
public class Quote {
@SerializedName("symbol")
@Expose
private String symbol;
@SerializedName("lastPrice")
@Expose
private double lastPrice;
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public double getLastPrice() {
return lastPrice;
}
public void setLastPrice(double lastPrice) {
this.lastPrice = lastPrice;
}
}
public class Stats {
@SerializedName("dividendRate")
@Expose
private double dividendRate;
public double getDividendRate() {
return dividendRate;
}
public void setDividendRate(double dividendRate) {
this.dividendRate = dividendRate;
}
}
how I suppose to handle the above response using retrofit? 我想如何使用改造处理上述响应?
- You just need to use the same class. - 你只需要使用同一个类。 just change the SerializedName("APPL") eg for Tesla SerializedName("TESLA") as we are getting the same response inside. 只需更改SerializedName(“APPL”),例如Tesla SerializedName(“TESLA”),因为我们在内部得到相同的响应。 so we dont need to create the whole class again and again. 所以我们不需要一次又一次地创建整个班级。
@SerializedName("APPL")
@Expose
private APPL aPPL;
@SerializedName("GOOGL")
@Expose
private APPL aPPL;
Does it make sense, if I can propose the following response format to the API designer, so that application developer can handle such response easier? 如果我可以向API设计者提出以下响应格式,那么它是否有意义,以便应用程序开发人员可以更轻松地处理此类响应?
More than an answer this might come across as a suggestion. 不仅仅是一个答案,这可能会作为一个建议。
The developer must have some reason behind the way the code was developed for creating the JSON response. 开发人员必须有一些理由支持开发代码以创建JSON响应的方式。 Questions you could ask are if some other application is also latching on to the response in this format, or if there are applications under development which require data in this format etc. If there are other applications that consume the data, then the answer by @Harminder Singh seems the right way. 您可能会问的问题是,其他一些应用程序是否也以这种格式锁定响应,或者是否有正在开发的应用程序需要此格式的数据等。如果还有其他应用程序使用这些数据,则答案为@ Harminder Singh似乎是正确的方式。 If this response is consumed just by the application being developed by you, I do not see any reason in having it the way you want it and you can ask the developer to change it to your specification. 如果仅由您开发的应用程序消耗此响应,我认为没有任何理由按照您希望的方式使用它,您可以要求开发人员将其更改为您的规范。
I manage to find a workaround. 我找到了解决方法。
It is using Map of String to BatchResponse 它使用Map of String to BatchResponse
It looks like 看起来像
Call<Map<String, BatchResponse>> batchMultipleQuoteStats(@Query("symbols") String symbols);
The BatchResponse
class is BatchResponse
类是
public class BatchResponse {
@SerializedName("quote")
@Expose
private Quote quote;
@SerializedName("stats")
@Expose
private Stats stats;
public Quote getQuote() {
return quote;
}
public void setQuote(Quote_ quote) {
this.quote = quote;
}
public Stats getStats() {
return stats;
}
public void setStats(Stats stats) {
this.stats = stats;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.