json - 在 Golang 中解析 JSON 以绘制多边形

标签 json parsing go

我正在用 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/

相关文章:

java - 为什么我的属性转换为pojo后无法识别

php - 解析 JSON 时定位同一标签的特定实例

ruby - 我如何检测 Ruby 中的文件结尾?

python - BeautifulSoup .text 方法返回没有分隔符的文本(\n、\r 等)

java - 如何使用 Java 库将标记的 YAML 对象转换为 JSON 对象?

go - 如何使用 gb 获得代码覆盖率?

string - Go:从两个字符或其他字符串之间检索字符串

javascript - 如何根据d3.js图形库中的节点id连接边与节点

javascript - 从浏览器向 Arduino 发送请求具有高延迟

python - 在 Go 中解密在 Python 中以 CFB 模式使用 AES 加密的内容