根据Golang中的对象值对JSON的解析进行排序

问题描述 投票:1回答:1

试图解析json并根据struct的一个值进行排序。我想根据custom_meta的part_num对json进行排序,我们该怎么做。代码如下:

type Maininfo struct {
    Id   string     `json:"id"`
    Meta []Metainfo `json:"meta"`
}


type Metainfo struct {
    Filename     string `json:"filename"`
    Custom_meta  string `json:"custom_meta"`
    Size         int    `json:"size"`
    Content_hash string `json:"content_hash"`
}

type Custom_meta struct {
    Part_num string `json:"part_num"`
    Part     int
}

func getMeta(body []byte) (*Maininfo, error) {
    var s = new(Maininfo)
    err := json.Unmarshal(body, &s)
    if err != nil {
        fmt.Println("whoops:", err)
    }
    return s, err
}


func getMetainfo(body []byte) (*Metainfo, error) {
    var s = new(Metainfo)
    err := json.Unmarshal(body, &s)
    if err != nil {
        fmt.Println("error", err)
    }
    return s, err
}

type AxisSorter []Metainfo

func (a AxisSorter) Len() int           { return len(a) }
func (a AxisSorter) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a AxisSorter) Less(i, j int) bool { return a[i].Custom_meta < a[j].Custom_meta }


type NameSorter []Metainfo

func (a NameSorter) Len() int           { return len(a) }
func (a NameSorter) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a NameSorter) Less(i, j int) bool { return a[i].Custom_meta < a[j].Custom_meta }

func main() {
s, err := getMeta([]byte(body))
    fmt.Println("Main stuff", s)

    var metaInfo []Metainfo
    metaInfo = s.Meta
}
    var customMeta CustomMeta

    sort.Sort(AxisSorter(metaInfo))
    fmt.Println("metaInfo sorted ", metaInfo)

    sort.Sort(NameSorter(metaInfo))
    fmt.Println("metaInfo sorted 2 ", metaInfo)

    sort.Slice(metaInfo, func(i, j int) bool {
        fmt.Println("meta ", metaInfo[i].Custom_meta)
        return metaInfo[i].Custom_meta < metaInfo[j].Custom_meta
      })

}

我无法根据part_num对代码进行排序,我们怎么能这样做,因为info不是一个单独的对象,而是一个字符串。我们如何解析字符串并根据int值对其进行排序。

json go
1个回答
0
投票

看起来这里的主要问题是“custom_meta”值是引用的JSON字符串而不是嵌套对象,这意味着它不能被解组为具有(可能)期望的“part_num”整数的对象。

理想情况下,您可以修复此数据的来源,以便它发出JSON对象而不是引用的JSON字符串;但是,如果这不可行,那么您可以执行以下操作。

  1. 让“Custom_meta”类型通过首先取消引用源字符串然后像往常一样解组来实现json.Umarshaler
  2. 通过嵌套的“Custom_meta.Part”字段单独或作为该类型的自定义解组的一部分对“Maininfo.Meta”进行排序。

例如(Go Playground):

type MainInfo struct {
  Id        string     `json:"id"`
  MetaInfos []MetaInfo `json:"meta"`
}

type MetaInfo struct {
  Filename    string     `json:"filename"`
  Custom      CustomMeta `json:"custom_meta"`
  Size        int        `json:"size"`
  ContentHash string     `json:"content_hash"`
}

type CustomMeta struct {
  PartNum int `json:"part_num"`
}

func (cm *CustomMeta) UnmarshalJSON(bs []byte) error {
  // Unquote the source string so we can unmarshal it.
  unquoted, err := strconv.Unquote(string(bs))
  if err != nil {
    return err
  }

  // Create an aliased type so we can use the default unmarshaler.
  type CustomMeta2 CustomMeta
  var cm2 CustomMeta2

  // Unmarshal the unquoted string and assign to the original object.
  if err := json.Unmarshal([]byte(unquoted), &cm2); err != nil {
    return err
  }
  *cm = CustomMeta(cm2)
  return nil
}

然后你可以像这样解析后排序:

var doc MainInfo
err := json.Unmarshal([]byte(jsonstr), &doc)
if err != nil {
  panic(err)
}
sort.Slice(doc.MetaInfos, func(i, j int) bool {
  p1 := doc.MetaInfos[i].Custom.PartNum
  p2 := doc.MetaInfos[j].Custom.PartNum
  return p1 < p2
})

当然,您也可以将排序作为“MainInfo”类型的自定义UnmarshalJSON方法的一部分执行。

© www.soinside.com 2019 - 2024. All rights reserved.