doulierong0334 2017-10-23 02:53 采纳率: 100%
浏览 103
已采纳

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

I'm parsing a large JSON file in Go where I need to take only specific items from the JSON.

It's usually a very large file, but eventually, it becomes this:

"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"
    },

I need to take the vertices and description of all of the "boundingPoly"'s in that are within "textAnnotations". I've looked at a few JSON parsing libraries but none seem to do exactly what I'm asking for.

  • 写回答

2条回答 默认 最新

  • dsjklb0205 2017-10-23 03:01
    关注

    You just need your go structure laid out correctly.

    Something like this:

    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"`
    }
    

    The rest is pretty straight forward: https://play.golang.org/p/ICtREyQyjF

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退