在 Kotlin 中解析嵌套 Map
我在解析 Kotlin 地图时遇到问题 - 我认为是 - Kotlin 地图。 SDK 通过创建 jsonStruct 然后提供一些数据 vallatestData = data.jsonStruct.fieldsMap
出来了。当我打印 latestData.toString()
时,我得到了这个(更改了值并使其更易于显示): 编辑:这是 google protobuf
{id=string_value: "id1"
, data=struct_value {
fields {
key: "key1"
value {
list_value {
values {
string_value: "v1"
}
values {
string_value: "v2"
}
}
}
}
fields {
key: "key17"
value {
struct_value {
fields {
key: "id2"
value {
string_value: "value2"
}
}
fields {
key: "id3"
value {
struct_value {
fields {
key: "key2"
value {
string_value: "info"
}
}
}
}
}
}
}
}
}
, someID=string_value: "someID"
}
我怎样才能将“v2”或“info”保存到变量中? “id1”可以用 latestData["id"]
保存,但我不知道如何执行其余操作,因为它是嵌套的。 谢谢。
I´m having a problem with parsing - what I think is- a Kotlin Map. A SDK gives me some data by making a jsonStruct and then aval latestData = data.jsonStruct.fieldsMap
out of it. When I print the latestData.toString()
I get this (changed values and made it simpler for showing purposes):
Edit: it´s a google protobuf
{id=string_value: "id1"
, data=struct_value {
fields {
key: "key1"
value {
list_value {
values {
string_value: "v1"
}
values {
string_value: "v2"
}
}
}
}
fields {
key: "key17"
value {
struct_value {
fields {
key: "id2"
value {
string_value: "value2"
}
}
fields {
key: "id3"
value {
struct_value {
fields {
key: "key2"
value {
string_value: "info"
}
}
}
}
}
}
}
}
}
, someID=string_value: "someID"
}
How can I e.g. save "v2" or "info" into variables? The "id1" can be saved with latestData["id"]
, but I couldn´t figure out how to do it for the rest since it´s nested.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议使用 klaxon 库解析 JSON。
如果这不能回答您的问题,您可以尝试使用 gson 将其转换为 Java 对象
I'd suggest parsing the JSON with the klaxon library.
If that doesn't answer you question, you could try to use gson to convert it to a Java object
工作原理如下:
works like this: