尝试解析 json 并根据结构的值之一进行排序。 我想根据 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 对代码进行排序,我们该怎么做,因为信息不是一个单独的对象,它是一个字符串。我们如何解析字符串并根据 int 值对其进行排序。
最佳答案
看起来这里的主要问题是“custom_meta”值是一个带引号的 JSON 字符串而不是一个嵌套对象,这意味着它不能被解码为一个具有(可能)所需的“part_num”整数的对象。
理想情况下,您可以修复此数据的来源,以便它发出一个 JSON 对象而不是带引号的 JSON 字符串;但是,如果这不可行,那么您可以执行以下操作。
- 拥有“Custom_meta”类型的工具
json.Umarshaler
首先取消引用源字符串,然后像往常一样解码。 - 按嵌套的“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
方法的一部分执行。
关于json - 在 Golang 中根据对象值对 JSON 的解析进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52301095/