尝试解析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值对其进行排序。
答案 0 :(得分:0)
似乎这里的主要问题是“ custom_meta”值是带引号的JSON字符串,而不是嵌套对象,这意味着它无法解组为带有(可能是)所需的“ part_num”整数的对象。
理想情况下,您可以修复此数据的源,以便它发出JSON对象而不是带引号的JSON字符串;但是,如果这不可行,则可以执行以下操作。
json.Umarshaler
。例如(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
方法中执行排序。