使用Gson将对象转换为List

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Gson将对象转换为List相关的知识,希望对你有一定的参考价值。

这是我的回复

{
"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;
}
}
答案

由于你无法猜测密钥,你应该迭代它们:

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

这只适用于按键,但我认为你有这个想法。

另一答案

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

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

以上是关于使用Gson将对象转换为List的主要内容,如果未能解决你的问题,请参考以下文章

Kotlin 使用 Gson 将 json 字符串转换为对象列表

如何使用 Gson 将 JSONArray 转换为 List?

如何使用 Gson 将简单对象转换为字符串? [关闭]

当从共享prefs获取时,GSON将先前存储的List转换为LinkedTreeMaps的ArrayList

如何将Java对象转换为JSON(Gson)?

用Gson将json转换为List<JavaBean>时,报declares multiple JSON fields named serialVersionUID