0
Retrofit Libararyを使用してJSONオブジェクトとSOME Time JSON Arrayを取得したときに直面する問題があります。 どうすればいいですか。Jsonを扱う方法時間がオブジェクトを取得するか、または一部の時間が更新を使用して配列を取得する
Retrofit Libararyを使用してJSONオブジェクトとSOME Time JSON Arrayを取得したときに直面する問題があります。 どうすればいいですか。Jsonを扱う方法時間がオブジェクトを取得するか、または一部の時間が更新を使用して配列を取得する
あなたの応答が
いくつかの回のような{
"accounts": {
"AccountType":
{
"accountId": 620430,
"active": true,
}
},
"code": "OK"
}
であり、いくつかの時間
{
"accounts": {
"AccountType": [
{
"accountId": 620430,
"active": true,
},
{
"accountId": 620330,
"active": true,
}
]
},
"code": "OK"
}
は、これらのコードを使用している場合、それは
static Gson gson = new GsonBuilder().registerTypeAdapter(Accounts.class, new LocationsDeserializer()).create();
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
;
public static class LocationsDeserializer implements JsonDeserializer<Accounts> {
@Override
public Accounts deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonElement monumentElement = json.getAsJsonObject().get("AccountType");
if (monumentElement.isJsonArray()) {
return new Accounts((AccountType[]) context.deserialize(monumentElement.getAsJsonArray(), AccountType[].class));
} else if (monumentElement.isJsonObject()) {
return new Accounts((AccountType) context.deserialize(monumentElement.getAsJsonObject(), AccountType.class));
} else {
throw new JsonParseException("Unsupported type of monument element");
}
}
}
とモデルクラスあなたの問題を解決することを願っています
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class AccountResponse {
@SerializedName("accounts")
@Expose
private Accounts accounts;
@SerializedName("code")
@Expose
private String code;
public Accounts getAccounts() {
return accounts;
}
public void setAccounts(Accounts accounts) {
this.accounts = accounts;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
-----------------------------------com.example.AccountType.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class AccountType {
@SerializedName("accountId")
@Expose
private Integer accountId;
@SerializedName("active")
@Expose
private Boolean active;
public Integer getAccountId() {
return accountId;
}
public void setAccountId(Integer accountId) {
this.accountId = accountId;
}
public Boolean getActive() {
return active;
}
public void setActive(Boolean active) {
this.active = active;
}
}
-----------------------------------com.example.Accounts.java-----------------------------------
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Accounts {
@SerializedName("AccountType")
@Expose
private List<AccountType> accountType = null;
public Accounts(AccountType ... ms) {
accountType = Arrays.asList(ms);
}
public List<AccountType> getAccountType() {
return accountType;
}
public void setAccountType(List<AccountType> accountType) {
this.accountType = accountType;
}
}
どのようなタイプのモデルクラスが作成されますか? – Vishal
モデルクラスを確認してください。 –
あなたのコードを貼り付けることができますか?あなたの問題について私は明らかにしています –