{"id":207055,"date":"2025-07-08T10:32:26","date_gmt":"2025-07-08T02:32:26","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207055\/"},"modified":"2025-07-08T10:32:26","modified_gmt":"2025-07-08T02:32:26","slug":"golang%e4%b8%ad%e6%a0%b9%e6%8d%ae%e5%af%b9%e8%b1%a1%e5%80%bc%e5%af%b9json%e8%a7%a3%e6%9e%90%e8%bf%9b%e8%a1%8c%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207055\/","title":{"rendered":"Golang\u4e2d\u6839\u636e\u5bf9\u8c61\u503c\u5bf9JSON\u89e3\u6790\u8fdb\u884c\u6392\u5e8f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>Golang\u4e2d\u6839\u636e\u5bf9\u8c61\u503c\u5bf9JSON\u89e3\u6790\u8fdb\u884c\u6392\u5e8f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-19 18:57:33<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGolang\u4e2d\u6839\u636e\u5bf9\u8c61\u503c\u5bf9JSON\u89e3\u6790\u8fdb\u884c\u6392\u5e8f\u300b<\/span>\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5c1d\u8bd5\u89e3\u6790 json \u5e76\u6839\u636e struct \u7684\u503c\u4e4b\u4e00\u8fdb\u884c\u6392\u5e8f\u3002 \u6211\u60f3\u6839\u636ecustom_meta\u7684part_num\u5bf9json\u8fdb\u884c\u6392\u5e8f\uff0c\u6211\u4eec\u5982\u4f55\u505a\u5230\u8fd9\u4e00\u70b9\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>type Maininfo struct {\n    Id   string     `json:\"id\"`\n    Meta []Metainfo `json:\"meta\"`\n}\n\n\ntype Metainfo struct {\n    Filename     string `json:\"filename\"`\n    Custom_meta  string `json:\"custom_meta\"`\n    Size         int    `json:\"size\"`\n    Content_hash string `json:\"content_hash\"`\n}\n\ntype Custom_meta struct {\n    Part_num string `json:\"part_num\"`\n    Part     int\n}\n\nfunc getMeta(body []byte) (*Maininfo, error) {\n    var s = new(Maininfo)\n    err := json.Unmarshal(body, &amp;s)\n    if err != nil {\n        fmt.Println(\"whoops:\", err)\n    }\n    return s, err\n}\n\n\nfunc getMetainfo(body []byte) (*Metainfo, error) {\n    var s = new(Metainfo)\n    err := json.Unmarshal(body, &amp;s)\n    if err != nil {\n        fmt.Println(\"error\", err)\n    }\n    return s, err\n}\n\ntype AxisSorter []Metainfo\n\nfunc (a AxisSorter) Len() int           { return len(a) }\nfunc (a AxisSorter) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }\nfunc (a AxisSorter) Less(i, j int) bool { return a[i].Custom_meta &lt; a[j].Custom_meta }\n\n\ntype NameSorter []Metainfo\n\nfunc (a NameSorter) Len() int           { return len(a) }\nfunc (a NameSorter) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }\nfunc (a NameSorter) Less(i, j int) bool { return a[i].Custom_meta &lt; a[j].Custom_meta }\n\nfunc main() {\ns, err := getMeta([]byte(body))\n    fmt.Println(\"Main stuff\", s)\n\n    var metaInfo []Metainfo\n    metaInfo = s.Meta\n}\n    var customMeta CustomMeta\n\n    sort.Sort(AxisSorter(metaInfo))\n    fmt.Println(\"metaInfo sorted \", metaInfo)\n\n    sort.Sort(NameSorter(metaInfo))\n    fmt.Println(\"metaInfo sorted 2 \", metaInfo)\n\n    sort.Slice(metaInfo, func(i, j int) bool {\n        fmt.Println(\"meta \", metaInfo[i].Custom_meta)\n        return metaInfo[i].Custom_meta &lt; metaInfo[j].Custom_meta\n      })\n\n}<\/pre>\n<p>\u6211\u65e0\u6cd5\u6839\u636epart_num\u5bf9\u4ee3\u7801\u8fdb\u884c\u6392\u5e8f\uff0c\u6211\u4eec\u8be5\u600e\u4e48\u505a\uff0c\u56e0\u4e3ainfo\u4e0d\u662f\u4e00\u4e2a\u5355\u72ec\u7684\u5bf9\u8c61\uff0c\u5b83\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u6211\u4eec\u5982\u4f55\u89e3\u6790\u5b57\u7b26\u4e32\u5e76\u6839\u636e int \u503c\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\u3002 <\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u770b\u8d77\u6765\u8fd9\u91cc\u7684\u4e3b\u8981\u95ee\u9898\u662f\u201ccustom_meta\u201d\u503c\u662f\u5e26\u5f15\u53f7\u7684 json \u5b57\u7b26\u4e32\u800c\u4e0d\u662f\u5d4c\u5957\u5bf9\u8c61\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u65e0\u6cd5\u88ab\u89e3\u7ec4\u5230\u5177\u6709\uff08\u5927\u6982\uff09\u6240\u9700\u201cpart_num\u201d\u6574\u6570\u7684\u5bf9\u8c61\u4e2d\u3002 p&gt; <\/p>\n<p>\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u4ee5\u4fee\u590d\u6b64\u6570\u636e\u7684\u6765\u6e90\uff0c\u4ee5\u4fbf\u5b83\u53d1\u51fa json \u5bf9\u8c61\u800c\u4e0d\u662f\u5e26\u5f15\u53f7\u7684 json \u5b57\u7b26\u4e32\uff1b\u4f46\u662f\uff0c\u5982\u679c\u8fd9\u4e0d\u53ef\u884c\uff0c\u90a3\u4e48\u60a8\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\u3002<\/p>\n<ol>\n<li>\u9996\u5148\u53d6\u6d88\u5f15\u7528\u6e90\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u7167\u5e38\u8fdb\u884c\u89e3\u7ec4\uff0c\u8ba9\u201ccustom_meta\u201d\u7c7b\u578b\u5b9e\u73b0 \u3002<\/li>\n<li>\u6309\u5d4c\u5957\u7684\u201ccustom_meta.part\u201d\u5b57\u6bb5\u5355\u72ec\u5bf9\u201cmaininfo.meta\u201d\u8fdb\u884c\u6392\u5e8f\uff0c\u6216\u4f5c\u4e3a\u8be5\u7c7b\u578b\u7684\u81ea\u5b9a\u4e49\u89e3\u7ec4\u5668\u7684\u4e00\u90e8\u5206\u3002<\/li>\n<\/ol>\n<p>\u4f8b\u5982\uff08\uff09\uff1a<\/p>\n<pre>type maininfo struct {\n  id        string     `json:\"id\"`\n  metainfos []metainfo `json:\"meta\"`\n}\n\ntype metainfo struct {\n  filename    string     `json:\"filename\"`\n  custom      custommeta `json:\"custom_meta\"`\n  size        int        `json:\"size\"`\n  contenthash string     `json:\"content_hash\"`\n}\n\ntype custommeta struct {\n  partnum int `json:\"part_num\"`\n}\n\nfunc (cm *custommeta) unmarshaljson(bs []byte) error {\n  \/\/ unquote the source string so we can unmarshal it.\n  unquoted, err := strconv.unquote(string(bs))\n  if err != nil {\n    return err\n  }\n\n  \/\/ create an aliased type so we can use the default unmarshaler.\n  type custommeta2 custommeta\n  var cm2 custommeta2\n\n  \/\/ unmarshal the unquoted string and assign to the original object.\n  if err := json.unmarshal([]byte(unquoted), &amp;cm2); err != nil {\n    return err\n  }\n  *cm = custommeta(cm2)\n  return nil\n}<\/pre>\n<p>\u7136\u540e\u4f60\u53ef\u4ee5\u50cf\u8fd9\u6837\u89e3\u6790\u540e\u6392\u5e8f\uff1a<\/p>\n<pre>var doc MainInfo\nerr := json.Unmarshal([]byte(jsonstr), &amp;doc)\nif err != nil {\n  panic(err)\n}\nsort.Slice(doc.MetaInfos, func(i, j int) bool {\n  p1 := doc.MetaInfos[i].Custom.PartNum\n  p2 := doc.MetaInfos[j].Custom.PartNum\n  return p1 &lt; p2\n})<\/pre>\n<p>\u5f53\u7136\uff0c\u60a8\u4e5f\u53ef\u4ee5\u5c06\u6392\u5e8f\u4f5c\u4e3a\u201cmaininfo\u201d\u7c7b\u578b\u7684\u81ea\u5b9a\u4e49 <code>unmarshaljson<\/code> \u65b9\u6cd5\u7684\u4e00\u90e8\u5206\u6765\u6267\u884c\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aGolang\u4e2d\u6839\u636e\u5bf9\u8c61\u503c\u5bf9JSON\u89e3\u6790\u8fdb\u884c\u6392\u5e8f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207055","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207055","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207055"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207055\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207055"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207055"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207055"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}