2016-04-14 28 views
0

Json nesnesini eşlemek için GSON ve JsonScheme2Pojo kullanmaya çalışıyorum.Aynı anahtarla birden çok json nesnesinde json eşlemesi nasıl yapılır

Şimdiye kadar bu sınıfı oluşturdum.

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; 
} 

} 

ve sonra ben sınıfa bu şekilde seslendi:

Gson gson = new Gson(); 
MPayTransaction mPayTransaction = gson.fromJson(jsonString, MPayTransaction.class); 

Bu çalışıyor ve sadece örnek MPayTransaction.getAmount() miktarın değeri elde etmek için aramaya gerek;

Merak ediyorum, birden çok json nesnesini nasıl eşleyebilirim? bu nasıl ardından

{ 
"mp_request_type": "donePayment", 
"status_code": "00", 
"amount": "1.10", 
"chksum": "23259ef7e03266ada789bf5a30465469", 
"pInstruction": 0 
} 

ve:

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

nasıl ben MPayTransaction.getAmount() den 13.10 olan 2 miktarın değerini alırım önceki json dizesi şu şekilde olduğunu varsayalım?

cevap

2

kullanım ArrayList <>, örneğin

jsonString:

[{ 
"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 için bu dizeyi ayrıştırmak <>:

arrayList.get(1).getAmount() 
:

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); 

sonra 2 miktarın değeri elde

+0

Benim için çalıştı. Json "java.lang.reflect.Type" ve "com.google.gson.reflect.TypeToken" kullandım. – JeromeC

İlgili konular