【发布时间】:2016-08-05 09:35:24
【问题描述】:
我一直在尝试使用 GSON 和 JsonScheme2Pojo 来映射我的 json 对象。
到目前为止,我已经创建了这个类。
public class MPayTransaction {
@SerializedName("mp_request_type")
@Expose
private String mpRequestType;
@SerializedName("status_code")
@Expose
private String statusCode;
@SerializedName("amount")
@Expose
private String amount;
@SerializedName("chksum")
@Expose
private String chksum;
@SerializedName("pInstruction")
/**
*
* @return
* The mpRequestType
*/
public String getMpRequestType() {
return mpRequestType;
}
/**
*
* @param mpRequestType
* The mp_request_type
*/
public void setMpRequestType(String mpRequestType) {
this.mpRequestType = mpRequestType;
}
/**
*
* @return
* The statusCode
*/
public String getStatusCode() {
return statusCode;
}
/**
*
* @param statusCode
* The status_code
*/
public void setStatusCode(String statusCode) {
this.statusCode = statusCode;
}
/**
*
* @return
* The amount
*/
public String getAmount() {
return amount;
}
/**
*
* @param amount
* The amount
*/
public void setAmount(String amount) {
this.amount = amount;
}
/**
*
* @return
* The chksum
*/
public String getChksum() {
return chksum;
}
/**
*
* @param chksum
* The chksum
*/
public void setChksum(String chksum) {
this.chksum = chksum;
}
/**
*
* @return
* The pInstruction
*/
public int getPInstruction() {
return pInstruction;
}
/**
*
* @param pInstruction
* The pInstruction
*/
public void setPInstruction(int pInstruction) {
this.pInstruction = pInstruction;
}
}
然后我以这种方式调用课程:
Gson gson = new Gson();
MPayTransaction mPayTransaction = gson.fromJson(jsonString, MPayTransaction.class);
这是有效的,我只需要调用例如MPayTransaction.getAmount() 来获取金额的值;
我一直在想,如何映射多个 json 对象?假设前面的 json 字符串是这样的:
{
"mp_request_type": "donePayment",
"status_code": "00",
"amount": "1.10",
"chksum": "23259ef7e03266ada789bf5a30465469",
"pInstruction": 0
}
然后这个怎么样:
{
"mp_request_type": "donePayment",
"status_code": "00",
"amount": "1.10",
"chksum": "23259ef7e03266ad23430465469",
"pInstruction": 0
},
{
"mp_request_type": "donePayment",
"status_code": "100",
"amount": "13.10",
"chksum": "23259ef7e03g2r32fa30465469",
"pInstruction": 0
}
我如何从MPayTransaction.getAmount() 获得第二个金额的值,即13.10?
【问题讨论】:
标签: android jsonschema2pojo gson