在Go中解组数组的JSON数组

chr*_*sdo 6 arrays json struct go

我想解析一些json数据。数据如下所示:

{“ id”:“ someId”,“ key_1”:“ value_1”,“ key_2”:“ value_2”,“ key_3”:“ value_3”,“点”:[[1487100466412,“ 50.032178”,“ 8.526018”,300 ,0.0,26,0],[1487100471563,“ 50.030869”,“ 8.525949”,300,0.0,38,0],[1487100475722,“ 50.028514”,“ 8.525959”,225,0.0,69,-900],[ 1487100480834,“ 50.025827”,“ 8.525793”,275,0.0,92,-262],...]}

我建立了一个go结构:

type SomeStruct struct {
   ID   string `json:"id"`
   Key1 string `json:"key_1"`
   Key2 string `json:"key_2"`
   Key3 string `json:"key_3"`
   Points []Point `json:"points"`
}

type Point struct {
   Timestamp int64 `json:"0"`
   Latitude float64 `json:"1,string"`
   Longitude float64 `json:"2,string"`
   Altitude int `json:"3"` 
   Value1 float64 `json:"4"`
   Value2 int `json:"5"`
   Value3 int `json:"6"`      
}
Run Code Online (Sandbox Code Playgroud)

我解组json数据

var track SomeStruct
error := json.Unmarshal(data,&track)
if(error != nil){
    fmt.Printf("Error while parsing data: %s", error)
}
Run Code Online (Sandbox Code Playgroud)

json:无法将数组解编为Point {someId value_1 value_2 value_3类型的Go值[{0 0 0 0 0 0 0 0} {0 0 0 0 0 0 0} {0 0 0 0 0 0 0} ...]}

因此,第一个JSON键正确解析,但是我无法弄清楚如何获取点数据,它是一个数组数组。

生成结构也是这里的建议之一,除了我不使用嵌套结构,而是使用单独的类型。使用建议的嵌套结构没有什么不同: JSON-to-Go

为此,我需要实现自己的Unmarshaller吗?

=======更新解决方案============

为Point结构实现UnmarshalJSON接口就足够了。下面的示例未包含正确的错误处理,但显示了方向。

游乐场的例子

package main

import (
    "encoding/json"
    "fmt"
    "strconv"
)

type SomeStruct struct {
    ID     string  `json:"id"`
    Key1   string  `json:"key_1"`
    Key2   string  `json:"key_2"`
    Key3   string  `json:"key_3"`
    Points []Point `json:"points"`
}

type Point struct {
    Timestamp int64
    Latitude  float64
    Longitude float64
    Altitude  int
    Value1    float64
    Value2    int16
    Value3    int16
}

func (tp *Point) UnmarshalJSON(data []byte) error {
    var v []interface{}
    if err := json.Unmarshal(data, &v); err != nil {
        fmt.Printf("Error whilde decoding %v\n", err)
        return err
    }
    tp.Timestamp = int64(v[0].(float64))
    tp.Latitude, _ = strconv.ParseFloat(v[1].(string), 64)
    tp.Longitude, _ = strconv.ParseFloat(v[2].(string), 64)
    tp.Altitude = int(v[3].(float64))
    tp.Value1 = v[4].(float64)
    tp.Value2 = int16(v[5].(float64))
    tp.Value3 = int16(v[6].(float64))

    return nil
}

func main() {

    const data =    `{"id":"someId","key_1":"value_1","key_2":"value_2","key_3":"value_3","points":[[1487100466412,"50.032178","8.526018",300,0.0,26,0],[1487100471563,"50.030869","8.525949",300,0.0,38,0],[1487100475722,"50.028514","8.525959",225,0.0,69,-900],[1487100480834,"50.025827","8.525793",275,0.0,92,-262]]}`

var something SomeStruct
json.Unmarshal([]byte(data), &something)

fmt.Printf("%v", something)
}
Run Code Online (Sandbox Code Playgroud)

Luc*_*nte 7

JSON:

[
  {
    "type": "Car",
    "wheels": 4
  },
  {
    "type": "Motorcycle",
    "wheels": 2
  }
]
Run Code Online (Sandbox Code Playgroud)

结构:

[
  {
    "type": "Car",
    "wheels": 4
  },
  {
    "type": "Motorcycle",
    "wheels": 2
  }
]
Run Code Online (Sandbox Code Playgroud)

解组器:

type Vehicle struct {
  Type   string
  Wheels int
}
Run Code Online (Sandbox Code Playgroud)

https://play.golang.org/p/5SfDH-XZt9J


Fli*_*mzy 3

我需要为此实现自己的 Unmarshaller 吗?

是的。

您尝试将数组解组为结构体 ( Point),这意味着您需要告诉 JSON 解组器数组值如何映射到结构体值。

另请注意,您的标签定义不正确Point。json 标签引用键名称,但数组没有键(在 JavaScript 中可以像有键一样访问它们,但这不是 JavaScript)。换句话说,json:"0"只有当您的 JSON 看起来像{"0":123}. 如果您实现自己的解组器,则可以删除这些 json 标签。