【发布时间】:2020-11-16 15:37:17
【问题描述】:
我有 json 如下所示,我是 kotlin 的新手,我尝试了所有示例,但在转换为 json 时无法设置嵌套类值
这是我的json
{"Init":{"MOP":[{"Id":"1","Type":"0","ProtocolVersion":"1.0","MopCode":"*NEXB","TerminalId":"'P400Plus-275008565'","IP":"'192.168.1.15'","Currency":"EUR"},{"Id":"2","Type":"0","ProtocolVersion":"1.0","MopCode":"*NEXF","TerminalId":"'P400Plus-275008565'","IP":"'10.0.0.0:901'","Currency":"EUR"}]}}
这是我的 POJO
class Root {
@JsonProperty("Init")
var init: Init? = null
}
class MOP {
@JsonProperty("Id")
var id: String? = null
@JsonProperty("Type")
var type: String? = null
@JsonProperty("ProtocolVersion")
var protocolVersion: String? = null
@JsonProperty("MopCode")
var mopCode: String? = null
@JsonProperty("TerminalId")
var terminalId: String? = null
@JsonProperty("IP")
var ip: String? = null
@JsonProperty("Currency")
var currency: String? = null
}
class Init {
@JsonProperty("MOP")
var mop: List<MOP>? = null
}
这是我的审判
val root: TestClass.Root = gson.fromJson(receiveString,TestClass.Root::class.java)
val initList = HashMap<String?,String?>()
if (root.init != null){
val mopList = root.init!!.mop
if (mopList != null) {
for (item in mopList){
initList.put(item.mopCode,item.id)
}
}
}
root.init 和 root.init.mop 始终为空
你能给我什么建议?
谢谢
【问题讨论】: