2016-04-14 7 views
0

Ich habe versucht, GSON und JsonScheme2Pojo zu verwenden, um meine JSON-Objekt zuzuordnen.Wie JSON-Mapping auf mehreren JSON-Objekt mit dem gleichen Schlüssel

Bisher habe ich diese Klasse erstellt.

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

} 

und dann rief ich die Klasse auf diese Weise:

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

Dies funktioniert und ich brauche nur MPayTransaction.getAmount() zum Beispiel nennen, den Wert der Menge zu erhalten;

Ich habe mich gefragt, wie kann ich mehrere Json-Objekt zuordnen? sagen wir mal die vorherigen JSON-String ist wie folgt:

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

und dann, wie etwa diese:

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

wie bekomme ich den Wert der zweiten Menge, die 13.10 von MPayTransaction.getAmount() ist?

Antwort

2

Verwendung Arraylist <>, zum Beispiel

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

diese Zeichenfolge analysieren zu 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); 

dann den Wert der zweiten Menge erhalten:

arrayList.get(1).getAmount() 
+0

Arbeitete für mich. Ich habe json "java.lang.reflect.Type" und "com.google.gson.reflect.TypeToken" verwendet. – JeromeC

Verwandte Themen