json - 使用 Go 解析 JSON 中包含 unicode 字符的键
问题描述
我有这样的 API 响应:
{
"Pass ✔": true
}
在 Go 中,我使用以下代码:
type Status struct {
Pass bool `json:"Pass ✔"`
}
// ...
var s Status
json.Unmarshal(body, &s)
fmt.Println(s.Pass) // false, where it should be true
如何正确解组此 JSON 文档?
解决方案
正如其他人提到的,目前不可能这样做。作为一种解决方法,您可以执行以下操作:
package main
import (
"encoding/json"
"fmt"
)
type status map[string]bool
func (s status) pass() bool {
return s["Pass ✔"]
}
func main() {
data := []byte(`{"Pass ✔": true}`)
var stat status
json.Unmarshal(data, &stat)
pass := stat.pass()
fmt.Println(pass) // true
}
推荐阅读
- javascript - php文件不会加载ajax请求
- java - Android SpeechRecorder 实时语音到 EditText 中的文本预览
- c++ - 如何通过stbi_write_png保存高清图片
- python - 将字母和数字的字符串转换为数组
- arrays - 为什么数组吞下的元素多于其容量?
- random - 为什么这个 rand use 语句在 rust 中起作用?
- node.js - UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“缓冲区”
- css - 如何在保持精确的 div 比率的同时阻止文本溢出到另一个 div?
- angular - 如何在 TypeScript 中正确使用 GeolocationPosition?
- html - 使用 BeautifulSoup 抓取非内联样式的 html 元素的 css 属性