使用gson将对象转换为列表

mzsu5hc0  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(446)

这是我的回答

{
"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 这是我的工作

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

igsr9ssn1#

因为你猜不到关键点,所以你应该迭代它们:

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

这只适用于钥匙,但我想你明白了。

hyrbngr7

hyrbngr72#

你应该使用Map,你可以很容易地把它转换成一个列表。
编辑:我不知道是否gson支持对列表。也许它能起作用。。。

相关问题