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 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试