gpt4 book ai didi

android - Gson 不会从 GSON 插件转换自动生成的类

转载 作者:行者123 更新时间:2023-11-29 22:53:34 29 4
gpt4 key购买 nike

我有以下 .json 文件 -

   {
"IDs":[
"1716136233",
"2030187302",
"2048978077",
"2366739744",
"2893154331",
"2933460506",
"3103364330",
"3156078714",
"3246372427",
"3917238269",
"4120130712",
"4219976332",
"4291368197",
"7157045144",
"97qIzbJhgPVeyPtMFIpbvtBjdz72",
"AhfGRayljDMx2G3UbEnxCFSjzV03",
"hEej5AN4DpYXR0kOklIAs9sUxwX2",
"5805506365",
"12915030523",
"AAcKGIJUYEYm9bzEmKfXSDK17Xf2",
"xVCKjchpAzXNbd4NNrEJgN50Ap42",
"E47aII272cVDI9m3fsT45V3XdP03",
"yqvCupYxHUVWVK2jmkAVvPcAXvr1",
"uFWidwQk0INUVdjXDSFZNjjHGrm2",
"1eHERZ2WiJTNTPF7y6RVDZFf65n1",
"Z4BzSFk5OpQK0VxPThyIfksOESv1",
"pNfP2xUCxbfslMTQeSU6Ifxr1rV2",
"ka2GdJ2cmRYHRgp8xyDqDRkZ9ys1",
"cAWsikLAj3XMsIaSpHzjc0ixlFV2",
"MzILthuDV9aWlvxqAAByMzfTUqK2",
"6617291130",
"RTzEt5DpBPTmQqzgOXXq2IGvoKL2",
"nz9py8DslHNbLgduvxCfLVOLoMg1",
"1Fici0ZStbXAccLKs5dh72xI0ZV2",
"13694871353",
"pyOB3Bhy4AaZ5GPUW4WLLmyD1Jd2",
"NvcjLcHwfCTjTbjVOjfafukPNp32",
"TSiTVkNY7qcFY9QxZ7r4CzKckLn1",
"4329923139",
"FgytkHqf0GddRqTyoguTh0aoxyG2",
"2uXKy54kvyY1cvCD9ut5CvzyCGY2",
"W4C3ueCQ0QYKNO8UDys4gt0RmNW2",
"ixU5ro9ZeldRVy0Q5vTJ5HSGHyB2",
"7308184033",
"Mnox4kviNhPWiZwQU3U3oi39DTf2",
"gxUIfqN9AFe5GY7Aj42pdlsbAk62",
"wuWZdHyqzBUOElbv6c5Pp4GGwwE2",
"1IwatVMcZKW30u9Ec6ofujhfqDQ2",
"Or014Dw6gTPTisolKfTsb0pxblh2",
"lbKFCsNTaKPFqkXG0gbogw6U5IQ2",
"DqbG3g4DdlQbhbzIZoWlyFkqsJF3",
"CEOMlUdMm2Q0FrqHlUBcWosLJt43",
"bPmSONjHNBcUIDgrPZZzrsOcP7N2",
"pp6ejp11AferP1zWbnKRcUXYqPm1",
"En4oViJj0keGl9SoKzCW4FRi8zc2",
"LwlmJ1wBXGdzBFmed7Np9AFQTkh1",
"TcqhUChpfGSgVGg4jGoEcbXMUwP2",
"4552B86LWQPCtekybJBd97dOP033",
"WV5PytXFFBgQzVnE3Ar9TjFzbuj2",
"Fh7ahcUJatNs0KLyUaOA7KYPekt1",
"DHmEB44BDrRl706AqmmYBUORWwB3",
"TKmfA6DfiZXApsCtVcu6LMIFZ7A3",
"yLT1HUat2ZPC4UfqGOI4edRjjgw1",
"DC3qrMXZa0Rs66WfyA3RCJIJqlo1",
"sznynbcWiMbVwADCfhvluRfyyX12",
"uWccGs4mkrRtuxsPt5D9Rmvhlzo2",
"kD6If8EeS2flhCBNAkJaN9QfSNr2",
]
}

我使用适用于 android studio 的 GsonFormatter 插件创建了以下类,粘贴了以下 .json 代码 -


public class ID {


private List<String> IDs;

public List<String> getIDs() {
return IDs;
}

public void setIDs(List<String> IDs) {
this.IDs = IDs;
}
}


但出于某种原因,当尝试使用 Gson 方法 fromJson(response, ID.class) 时,我得到的是 null 而不是为我自动生成的类型 -


Gson gson = new Gson();
ID id = gson.fromJson(response, ID.class);
Log.d("id - ", id.toString());


它打印到日志“id - null”

为什么?

最佳答案

试试下面的代码,把它翻译成java:

   data class Data(@SerializedName("IDs") val iDs: List<String>) {

companion object {
fun fromJson(json: String): Data {
val data = Gson().fromJson(json, Data::class.java)

return data
}
}

}

使用:

val data = Data.fromJson(jsonString)

关于android - Gson 不会从 GSON 插件转换自动生成的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57658847/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com