英文:
how to Flatten json null to empty string in golang
问题
我正在寻找一个用于将null JSON值转换为空字符串("")的Go库或解决方案,从
{
"foo": {
"jim": null
}
}
转换为
map[foo.jim:""]
目前,它在我的用例中被忽略了。有人可以帮我吗?
示例代码:https://go.dev/play/p/9hnMEa6QA2O
你可以看到我得到的输出是
map[fee:bar]
但我想要的是
map[foo.jim:"" fee:bar]
英文:
I am searching for go library or work around to flatten the null json value to empty string (""),
from
`{
"foo": {
"jim":null
}
}`
to
map[foo.jim:""]
as of now its being ignored for my use case.
can anybody help me with this.
example code https://go.dev/play/p/9hnMEa6QA2O
you can see that i get the output
map[fee:bar]
but i want
map[foo.jim:"" fee:bar]
答案1
得分: 0
在查看代码后,需要在switch语句中检查nil而不是忽略它。
default:
if v == nil {
flatMap[newKey] = ""
} else {
flatMap[newKey] = fmt.Sprintf("%v", v)
}
英文:
after going through the code,
had to check for nil instead of ignoring it in switch case.
default:
if v == nil {
flatMap[newKey] = ""
} else {
flatMap[newKey] = fmt.Sprintf("%v", v)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论