dtg25862 2018-08-22 22:47
浏览 88
已采纳

我如何在切片内进行json解组切片

I am trying to unmarshal some pretty ugly json but can't figure out how. I have:

package main

import "fmt"
import "encoding/json"

type PublicKey struct {
    ID     int    `json:"id"`
    Key    string `json:"key"`
    MyData []struct {
        ID    string `json:"id"`
        Value int    `json:"value"`
    }
}

func main() {
    b := `[
  {
    "id": 1,
    "key": "my_key"
  },
  [
    {
      "id": "some_id",
      "value": 12
    },
    {
      "id": "anorther_id",
      "value": 13
    }
  ]
]`

    var pk []PublicKey
    err := json.Unmarshal([]byte(b), &pk)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(pk)

}

For the result I am getting:

[{1 my_key []} {0  []}]

The second slice is empty when it shouldn't be.

EDIT: The error I get is:

json: cannot unmarshal array into Go struct field PublicKey.key of type main.PublicKey

https://play.golang.org/p/cztXOchiiS5

  • 写回答

2条回答 默认 最新

  • dongshi2588 2018-08-23 03:05
    关注

    That is some truly hideous JSON! I have two approaches to handling the mixed array elements and I like the 2nd one better. Here's the first approach using interface and a type switch:

    package main
    
    import (
        "encoding/json"
        "errors"
        "fmt"
    )
    
    type PublicKey struct {
        ID  int    `json:"id"`
        Key string `json:"key"`
    }
    
    type MyData struct {
        ID    string `json:"id"`
        Value int    `json:"value"`
    }
    
    type MixedData struct {
        Key    []PublicKey
        MyData [][]MyData
    }
    
    func (md *MixedData) UnmarshalJSON(b []byte) error {
        md.Key = []PublicKey{}
        md.MyData = [][]MyData{}
        var obj []interface{}
        err := json.Unmarshal([]byte(b), &obj)
        if err != nil {
            return err
        }
        for _, o := range obj {
            switch o.(type) {
            case map[string]interface{}:
                m := o.(map[string]interface{})
                id, ok := m["id"].(float64)
                if !ok {
                    return errors.New("public key id must be an int")
                }
                pk := PublicKey{}
                pk.ID = int(id)
                pk.Key, ok = m["key"].(string)
                if !ok {
                    return errors.New("public key key must be a string")
                }
                md.Key = append(md.Key, pk)
            case []interface{}:
                a := o.([]interface{})
                myData := make([]MyData, len(a))
                for i, x := range a {
                    m, ok := x.(map[string]interface{})
                    if !ok {
                        return errors.New("data array contains unexpected object")
                    }
                    val, ok := m["value"].(float64)
                    if !ok {
                        return errors.New("data value must be an int")
                    }
                    myData[i].Value = int(val)
                    myData[i].ID, ok = m["id"].(string)
                    if !ok {
                        return errors.New("data id must be a string")
                    }
                    md.MyData = append(md.MyData, myData)
                }
            default:
                // got something unexpected, handle somehow
            }
        }
        return nil
    }
    
    func main() {
        b := `[
      {
        "id": 1,
        "key": "my_key"
      },
      [
        {
          "id": "some_id",
          "value": 12
        },
        {
          "id": "another_id",
          "value": 13
        }
      ]
    ]`
    
        m := MixedData{}
        err := json.Unmarshal([]byte(b), &m)
        if err != nil {
            fmt.Println(err)
        }
        fmt.Println(m)
    
    }
    

    https://play.golang.org/p/g8d_AsH-pYY

    Hopefully there aren't any unexpected other elements, but they can be handled similarly.

    Here is the second that relies more on Go's internal JSON parsing with the help of json.RawMessage. It makes the same assumptions about the contents of the array. It assumes that any objects will Unmarshal into PublicKey instances and any arrays consist of only MyData instances. I also added how to marshal back into the target JSON for symmetry:

    package main
    
    import (
        "encoding/json"
        "fmt"
        "os"
    )
    
    type PublicKey struct {
        ID  int    `json:"id"`
        Key string `json:"key"`
    }
    
    type MyData struct {
        ID    string `json:"id"`
        Value int    `json:"value"`
    }
    
    type MixedData struct {
        Keys   []PublicKey
        MyData [][]MyData
    }
    
    func (md *MixedData) UnmarshalJSON(b []byte) error {
        md.Keys = []PublicKey{}
        md.MyData = [][]MyData{}
        obj := []json.RawMessage{}
        err := json.Unmarshal([]byte(b), &obj)
        if err != nil {
            return err
        }
        for _, o := range obj {
            switch o[0] {
            case '{':
                pk := PublicKey{}
                err := json.Unmarshal(o, &pk)
                if err != nil {
                    return err
                }
                md.Keys = append(md.Keys, pk)
            case '[':
                myData := []MyData{}
                err := json.Unmarshal(o, &myData)
                if err != nil {
                    return err
                }
                md.MyData = append(md.MyData, myData)
            default:
                // got something unexpected, handle somehow
            }
        }
        return nil
    }
    
    func (md *MixedData) MarshalJSON() ([]byte, error) {
        out := make([]interface{}, len(md.Keys)+len(md.MyData))
        i := 0
        for _, x := range md.Keys {
            out[i] = x
            i++
        }
        for _, x := range md.MyData {
            out[i] = x
            i++
        }
        return json.Marshal(out)
    }
    
    func main() {
        b := `[
      {
        "id": 1,
        "key": "my_key"
      },
      [
        {
          "id": "some_id",
          "value": 12
        },
        {
          "id": "another_id",
          "value": 13
        }
      ]
    ]`
    
        m := MixedData{}
        err := json.Unmarshal([]byte(b), &m)
        if err != nil {
            fmt.Println(err)
            os.Exit(1)
        }
        fmt.Println(m)
    
        enc := json.NewEncoder(os.Stdout)
        enc.SetIndent("", "    ")
        if err := enc.Encode(m); err != nil {
            fmt.Println(err)
            os.Exit(1)
        }
    }
    

    https://play.golang.org/p/ryZzaWKNcN0

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

报告相同问题?

悬赏问题

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