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

在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 代写uni代码,app唤醒
  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下: