【问题标题】:How to do json mapping on multiple json object with same key如何在具有相同键的多个 json 对象上进行 json 映射
【发布时间】: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


    【解决方案1】:

    使用 ArrayList,例如

    json字符串:

    [{
    "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
    }]
    

    将此字符串解析为 ArrayList:

    Gson gson = new Gson();
    Type collectionType = new TypeToken<Collection<MPayTransaction>>(){}.getType();
    Collection<MPayTransaction> collObj = gson.fromJson(jsonString, collectionType);
    ArrayList<MPayTransaction> arrayList = new ArrayList<MPayTransaction>(collObj);
    

    然后获取第二个金额的值:

    arrayList.get(1).getAmount()
    

    【讨论】:

    • 为我工作。我使用了 json "java.lang.reflect.Type" 和 "com.google.gson.reflect.TypeToken"。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2019-12-26
    相关资源
    最近更新 更多