我有一个GatewayModel类,使用GSON将json字符串反序列化。这是正确完成的。
public class GatewayModel {
private String title;
private String body;
protected Common common;
private ArrayList<Gateway1> gateways;
private Note note;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public ArrayList<Gateway1> getGateways() {
return gateways;
}
public void setGateways(ArrayList<Gateway1> gateways) {
this.gateways = gateways;
}
public Common getCommon() {
return common;
}
public void setCommon(Common common) {
this.common = common;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public Note getNote() {
return note;
}
public void setNote(Note note) {
this.note = note;
}
}
SUBCLASS
public class Gateway extends GatewayModel implements Serializable {
private String name;
private String image;
private String button;
private Form form;
private Self self;
private transient ArrayList<Element> preparedElements = new ArrayList<>();
/*
* Prepares elements from json - rejects invalid elements & adds common elements
* */
public ArrayList<Element> prepareGatewayData() {
ArrayList<Element> elements = this.getForm().getElements();
ArrayList<Element> elementsPrepared = new ArrayList<>();
if (elements == null) {
return null;
}
Common common=this.getCommon();//NULL VALUE
// elements.addAll(getCommon().getElement());
for (int i = 0; i < elements.size(); i++) {/
Element element = elements.get(i);
if (element.hasValidType()) {
elementsPrepared.add(element);
}
//
}
this.setPreparedElements(elementsPrepared);
return elementsPrepared;
}
}
GatewayModel.common obj(超类)的值返回为 即使GatewayModel.common正确,也可以从子类(Gateway)返回null 从GatewayModel访问时的值。
Gson反序列化
GatewayModel gatewayModel = null;
Gson gson = new Gson();
try {
gatewayModel = gson.fromJson(response.getResponse(), GatewayModel.class);
} catch (Exception e) {
e.printStackTrace();
}
if (gatewayModel != null && gatewayModel.getGateways() != null) {
gatewaysList=gatewayModel.getGateways();
}
我不确定这是否是gson问题或继承问题。我已经提到了
Object has null values when subclass attempts to use it. Why?
修改
这是json
{
"title": "Deposit",
"body": "Deposits are credited immediately ",
"common": {
"elements": {
"national_id": {
"type": "text",
"label": "National ID",
"rules": "required",
"value": ""
},
"terms_and_condition": {
"type": "text",
"label": "I understand that third party deposit is not allowed, ",
"rules": "required",
"value": ""
},
"button": {
"type": "button",
"label": "Next Step",
"rules": "",
"value": ""
}
}
},
"gateways": [
{
"name": "R",
"image": "https://*******/Rfupay.png",
"button": "Deposit",
"form": {
"title": "R - payment gateway",
"body": {
"p": [
"Processing fee: w!!",
"Withdrawal fee: No fees enk",
"Transaction limits: minimum $0 per transaction, "
]
},
"url": "https://****/api/v1/deposit/gateways/16",
"method": "POST",
"elements": {
"amount": {
"type": "text",
"label": "Amount",
"rules": "required|numeric|between:0,150000",
"value": ""
},
"currency_type": {
"type": "select",
"label": "Currency",
"rules": "required",
"value": "",
"options": {
"USD": "USD"
}
},
"bank_id": {
"type": "select",
"label": "Bank Name",
"value": "",
"options": {
"0011": " Agricultural Bank ",
},
"rules": "required|in:00017,00021,00004,00003,00032,00013,00016,00005,00057,00083,00006,00051,00054,00041"
},
"card_type": {
"type": "select",
"label": "Card Type",
"value": "",
"options": {
"01": " debit card"
},
"rules": "required|in:01"
},
"national_id": "common.elements.national_id",
"terms_and_condition": "common.elements.terms_and_condition",
"button": "common.elements.button"
}
},
"self": {
"url": "https://******/api/v1/deposit/gateways/16",
"method": "GET"
}
},
{
"name": "A",
"image": "https://*****/assets/images/AlgoCharge.png",
"button": "Deposit",
"form": {
"title": "A - payment gateway",
"body": {
"p": [
"Processing fee: 4%,!",
"Withdrawal fee: No fees k",
"Transaction limits: minimum $0 per transaction,"
]
},
"url": "https://***/api/v1/deposit/gateways/10",
"method": "POST",
"elements": {
"amount": {
"type": "text",
"label": "Amount",
"rules": "required|numeric|between:0,150000",
"value": ""
},
"currency_type": {
"type": "select",
"label": "Currency",
"rules": "required",
"value": "",
"options": {
"USD": "USD"
}
},
"national_id": "common.elements.national_id",
"terms_and_condition": "common.elements.terms_and_condition",
"button": "common.elements.button"
}
},
"self": {
"url": "https://*********/api/v1/deposit/gateways/10",
"method": "GET"
}
},
{
"name": "S",
"image": "https://*****/assets/images/Skrill.png",
"button": "Deposit",
"form": {
"title": "S - payment gateway",
"body": {
"p": [
"Processing fee: 3%, !!",
"Withdrawal fee: No fees ",
"Transaction limits: minimum $0 per transaction,"
]
},
"url": "https://*****/api/v1/deposit/gateways/11",
"method": "POST",
"elements": {
"amount": {
"type": "text",
"label": "Amount",
"rules": "required|numeric|between:0,150000",
"value": ""
},
"currency_type": {
"type": "select",
"label": "Currency",
"rules": "required",
"value": "",
"options": {
"USD": "USD"
}
},
"national_id": "common.elements.national_id",
"terms_and_condition": "common.elements.terms_and_condition",
"button": "common.elements.button"
}
},
"self": {
"url": "https://******/api/v1/deposit/gateways/11",
"method": "GET"
}
}
],
"note": {
"title": "Note:",
"body": [
"Priority card deposit and redemption services will resume by 01 August 2016",
"Master/Visa service is not applicable to card issues by banks of ."
]
}
}
编辑2
某些字符串的值有些变化我json,不能分享确切的json字符串,它不符合我公司的规范