使用Gson将对象转换为列表

时间:2018-12-06 12:22:17

标签: java android json kotlin gson

这是我的回应

{
"entity": "methods",
"card": true,
"debit_card": true,
"credit_card": true,
"amex": true,
"netbanking": {
"AIRP": "Airtel Payments Bank",
"ALLA": "Allahabad Bank",
"ANDB": "Andhra Bank",
"UTIB": "Axis Bank",
"BBKM": "Bank of Bahrein and Kuwait",
"BARB_R": "Bank of Baroda - Retail Banking",
"BKID": "Bank of India",
"MAHB": "Bank of Maharashtra",
"CNRB": "Canara Bank",
"CSBK": "Catholic Syrian Bank",
"CBIN": "Central Bank of India",
"CIUB": "City Union Bank",
"CORP": "Corporation Bank",
"COSB": "Cosmos Co-operative Bank",
"DCBL": "DCB Bank",
"BKDN": "Dena Bank",
"DEUT": "Deutsche Bank",
"DBSS": "Development Bank of Singapore",
"DLXB": "Dhanlaxmi Bank",
"ESFB": "Equitas Small Finance Bank",
"FDRL": "Federal Bank",
"HDFC": "HDFC Bank",
"ICIC": "ICICI Bank",
"IBKL": "IDBI",
"IDFB": "IDFC Bank",
"IDIB": "Indian Bank",
"IOBA": "Indian Overseas Bank",
"INDB": "Indusind Bank",
"JAKA": "Jammu and Kashmir Bank",
"JSBP": "Janata Sahakari Bank (Pune)",
"KARB": "Karnataka Bank",
"KVBL": "Karur Vysya Bank",
"KKBK": "Kotak Mahindra Bank",
"LAVB_C": "Lakshmi Vilas Bank - Corporate Banking",
"LAVB_R": "Lakshmi Vilas Bank - Retail Banking",
"NKGS": "NKGSB Co-operative Bank",
"ORBC": "Oriental Bank of Commerce",
"PMCB": "Punjab & Maharashtra Co-operative Bank",
"PSIB": "Punjab & Sind Bank",
"PUNB_C": "Punjab National Bank - Corporate Banking",
"PUNB_R": "Punjab National Bank - Retail Banking",
"RATN": "RBL Bank",
"SRCB": "Saraswat Co-operative Bank",
"SVCB": "Shamrao Vithal Co-operative Bank",
"SIBL": "South Indian Bank",
"SCBL": "Standard Chartered Bank",
"SBBJ": "State Bank of Bikaner and Jaipur",
"SBHY": "State Bank of Hyderabad",
"SBIN": "State Bank of India",
"SBMY": "State Bank of Mysore",
"STBP": "State Bank of Patiala",
"SBTR": "State Bank of Travancore",
"SYNB": "Syndicate Bank",
"TMBL": "Tamilnadu Mercantile Bank",
"TNSC": "Tamilnadu State Apex Co-operative Bank",
"UCBA": "UCO Bank",
"UBIN": "Union Bank of India",
"UTBI": "United Bank of India",
"VIJB": "Vijaya Bank",
"YESB": "Yes Bank"
},
"wallet": {
  "payzapp": true,
  "olamoney": true,
  "freecharge": true,
  "mpesa": true
},
"emi": false,
"upi": true,
"upi_intent": true
}

我想使用List将其转换为自定义对象类型的Gson

这是我的POJO,位于

public class RazorpayNetbanking {
@Expose
private String name;
@Expose
private String value;

public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}

public String getValue() {
    return this.value;
}

public void setValue(String value) {
    this.value = value;
}
}

2 个答案:

答案 0 :(得分:0)

由于您无法猜测密钥,因此应在它们上进行迭代:

val json: JsonObject // assuming
json.keySet().forEach {  key ->
    val value = json.get(key).asString
    // add to list (key, value)
}

这仅适用于钥匙,但我认为您已经明白了。

答案 1 :(得分:0)

您应该使用地图,并且可以轻松地将其转换为列表。

编辑:我不知道gson是否支持配对列表。也许可以用...