如何将JSON读为映射[字符串,字符串]?
解析JSON映射[String,String]时,我会遇到问题。这个想法是将嵌套的对象解析为“键”。
我们可以格式化此输入JSON:
{
"key-1":{
"sub-key-1-1":{
"sub-key-1-1-1":"value111"
},
"sub-key-1-2":{
"sub-key-1-2-1":"value121"
}
},
"key-2":{
"sub-key-2-1":{
"sub-key-2-1-1":"value211"
}
}
}
以将其作为映射[String,String]:
("key-1.sub-key-1-1.sub-key-1-1-1"->"value111",
"key-1.sub-key-1-2.sub-key-1-2-1"->"value121",
"key-2.sub-key-2-1.sub-key-2-1-1"->"value211")
I got the problem when parsing JSON to Map[String, String]. The idea is to parse the nested object to a key separate by "."
Can we format this input JSON:
{
"key-1":{
"sub-key-1-1":{
"sub-key-1-1-1":"value111"
},
"sub-key-1-2":{
"sub-key-1-2-1":"value121"
}
},
"key-2":{
"sub-key-2-1":{
"sub-key-2-1-1":"value211"
}
}
}
to this result as Map[String,String]:
("key-1.sub-key-1-1.sub-key-1-1-1"->"value111",
"key-1.sub-key-1-2.sub-key-1-2-1"->"value121",
"key-2.sub-key-2-1.sub-key-2-1-1"->"value211")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以这样考虑,如果JSValue是JSOBJECT,则需要提取键值并将其附加到函数的当前状态,如果是其他功能,您只需将它们添加到函数的状态中,然后退还它们。递归思考:
请参阅在
You can think about it this way, if the JsValue is a JsObject, you need to extract the key values and append them to the current state of your function, if it's anything else, you can just add them to the state of your function, and then return them. Thinking recursively:
See the code running on scastie