我正在用 Go 解析一个大型 JSON 文件,我只需要从 JSON 中提取特定的项目。
它通常是一个非常大的文件,但最终,它变成了这样:
"textAnnotations": [
{
"boundingPoly": {
"vertices": [
{
"x": 136,
"y": 119
},
{
"x": 5606,
"y": 119
},
{
"x": 5606,
"y": 3985
},
{
"x": 136,
"y": 3985
}
]
},
"description": "Description",
"locale": "en"
},
{
"boundingPoly": {
"vertices": [
{
"x": 3420,
"y": 122
},
{
"x": 3439,
"y": 122
},
{
"x": 3439,
"y": 144
},
{
"x": 3420,
"y": 144
}
]
},
"description": "10"
},
{
"boundingPoly": {
"vertices": [
{
"x": 4106,
"y": 119
},
{
"x": 4128,
"y": 119
},
{
"x": 4128,
"y": 141
},
{
"x": 4106,
"y": 141
}
]
},
"description": "12"
},
我需要获取“textAnnotations”中所有“boundingPoly”的顶点和描述。我查看了一些 JSON 解析库,但似乎没有一个能完全满足我的要求。
最佳答案
您只需要正确布局您的 go 结构。
像这样:
type YourType struct {
TextAnnotations []struct {
BoundingPoly struct {
Vertices []struct {
X int `json:"x"`
Y int `json:"y"`
} `json:"vertices"`
} `json:"boundingPoly"`
Description string `json:"description"`
Locale string `json:"locale,omitempty"`
} `json:"textAnnotations"`
}
剩下的就很简单了:https://play.golang.org/p/ICtREyQyjF
关于json - 在 Golang 中解析 JSON 以绘制多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46881303/