{"id":4151,"date":"2018-10-31T07:53:24","date_gmt":"2018-10-30T23:53:24","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4151"},"modified":"2018-10-31T07:53:24","modified_gmt":"2018-10-30T23:53:24","slug":"go%e8%af%ad%e8%a8%80%e5%ad%a6%e4%b9%a011-%e5%a6%82%e4%bd%95%e8%a7%a3%e6%9e%90json%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4151.html","title":{"rendered":"Go\u8bed\u8a00\u5b66\u4e60#11-\u5982\u4f55\u89e3\u6790json\u5b57\u7b26\u4e32"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u73b0\u5728\u7684json\u683c\u5f0f\u7528\u7684\u975e\u5e38\u4e4b\u591a\uff0c\u6bd4\u5982\uff1a\u914d\u7f6e\u6587\u4ef6\uff0c\u63a5\u53e3\u4e4b\u95f4\u7684\u6570\u636e\u4f20\u9012\u3002\u6240\u4ee5\uff0c\u7528Go \u8bed\u8a00\u6765\u5bf9json\u5b57\u7b26\u4e32\u8fdb\u884c\u8bfb\u53d6\u3001\u89e3\u6790\u3001\u5728\u5404\u7c7b\u578b\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u8fd9\u91cc\u7b80\u5355\u6574\u7406\u603b\u7ed3\u4e00\u4e0b\uff0c\u65b9\u4fbf\u4ee5\u540e\u7528\u5230\u7684\u65f6\u5019\u53c2\u8003\u3002<\/p>\n<h4 id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<p>Go \u8bed\u8a00\u5728\u5185\u7f6e\u7684&#8221;encoding\/json&#8221;\u5e93\u4e2d\u63d0\u4f9b\u4e86Marshal\u548cUnmarshal\u51fd\u6570\u4ee5\u652f\u6301\uff08\u5df2\u77e5\u7ed3\u6784\u7684\uff09struct\u7ed3\u6784\u4f53\u548cjson\u5b57\u7b26\u4e32\u4e4b\u95f4\u7684\u8f6c\u6362\uff1a<\/p>\n<pre class=\"lang:default decode:true \">\/\/ \u5c06 struct\u7ed3\u6784\u4f53 \u8f6c\u6210 json\u683c\u5f0f\u4e32\r\nfunc Marshal(v interface{}) ([]byte, error)\r\n\/\/ Marshal returns the JSON encoding of v.\r\n\r\n\/\/ \u5c06 json\u683c\u5f0f\u4e32 \u8f6c\u6210 struct\u7ed3\u6784\u4f53\r\nfunc Unmarshal(data []byte, v interface{}) error\r\n\/\/ Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.\r\n<\/pre>\n<h6><span style=\"color: #ff0000;\"><strong>1\u3001\u5c06\u56fa\u5b9a\u683c\u5f0f\u7684json\u5b57\u7b26\u4e32\u8f6c\u6362\u6210struct\u7ed3\u6784\u4f53<\/strong><\/span><\/h6>\n<p>\u6765\u4e00\u4e2a\u5b9e\u9645\u70b9\u7684\u4f8b\u5b50\u5c31\u662f\uff1a\u901a\u8fc7\u63a5\u53e3\u00a0<a href=\"http:\/\/ipinfo.io\/json\">http:\/\/ipinfo.io\/json<\/a>\u00a0\u83b7\u53d6\u81ea\u5df1\u7f51\u7edc\u51fa\u53e3\u516c\u7f51IP\u5730\u5740\u7684\u76f8\u5173\u4fe1\u606f\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:default decode:true \">{\r\n  \"ip\": \"115.21.14.38\",\r\n  \"city\": \"Xinghuo\",\r\n  \"region\": \"Beijing\",\r\n  \"country\": \"CN\",\r\n  \"loc\": \"58.2500,36.1670\",\r\n  \"org\": \"AS165256 CN CARE NETWORK LTD\"\r\n}<\/pre>\n<p>\u7136\u540e\u4f60\u60f3\u4ece\u4e0a\u9762\u8fd9\u6bb5json\u5b57\u7b26\u4e32\u4e2d\u89e3\u6790\u51faIP\u5730\u5740\u548c\u5730\u533aregion\u51fa\u6765\uff0c\u56e0\u4e3a\u8fd4\u56de\u5185\u5bb9\u7684\u683c\u5f0f\u90fd\u662f\u56fa\u5b9a\u7684\uff0c\u6240\u4ee5\u6211\u4eec<span style=\"color: #ff0000;\"><strong>\u53ef\u4ee5\u5148\u5bf9\u7740\u5185\u5bb9\u5b9a\u4e49\u4e00\u4e2a\u7ed3\u6784\u4f53IpInfo\u51fa\u6765\uff0c\u501f\u52a9Unmarshal\u51fd\u6570\u5c06json\u5b57\u7b26\u4e32\u89e3\u6790\u5230\u4e00\u4e2aIpInfo\u7ed3\u6784\u4f53\u53d8\u91cf\u4e2d<\/strong><\/span>\uff0c\u7136\u540e\u8fdb\u884c\u4e0b\u4e00\u6b65\u7684\u5904\u7406\uff1a<\/p>\n<pre class=\"lang:default decode:true \">package main\r\n\r\nimport (\r\n    \"encoding\/json\"\r\n    \"log\"\r\n)\r\n\r\n\/\/ \u5728 tag \u4e2d\u5b9a\u4e49\u7684\u540d\u79f0\u4e0d\u9700\u8981\u9996\u5b57\u6bcd\u5927\u5199\uff0c\u4e14\u5bf9\u5e94\u7684 json \u4e32\u4e2d\u5b57\u6bb5\u540d\u79f0\u4ecd\u7136\u5927\u5c0f\u5199\u4e0d\u654f\u611f\r\ntype IpInfo struct {\r\n    Ip      string `json:\"ip\"`\r\n    City    string `json:\"city\"`\r\n    Region  string `json:\"region\"`\r\n    Country string `json:\"country\"`\r\n    Loc     string `json:\"loc\"`\r\n    Org     string `json:\"org\"`\r\n}\r\n\r\nfunc main() {\r\n    jsonStr := `{\r\n  \"ip\": \"115.21.14.38\",\r\n  \"city\": \"Xinghuo\",\r\n  \"region\": \"Beijing\",\r\n  \"country\": \"CN\",\r\n  \"loc\": \"58.2500,36.1670\",\r\n  \"org\": \"AS165256 CN CARE NETWORK LTD\"\r\n}`\r\n    log.Printf(\"jsonStr:\\n%s\\n\", jsonStr)\r\n\r\n    var ipInfo IpInfo\r\n    json.Unmarshal([]byte(jsonStr), &amp;ipInfo)\r\n    log.Printf(\"[ipInfo]value: %v\\ntype: %T\\n\", ipInfo, ipInfo) \/\/ type: main.IpInfo\r\n    log.Println(\"ipInfo.Ip:\", ipInfo.Ip)\r\n    \/\/ log.Println(\"ipInfo.ip:\", ipInfo.ip) \/\/ ipInfo.ip undefined\r\n}<\/pre>\n<h6><span style=\"color: #ff0000;\"><strong>2.\u5c06struct\u7ed3\u6784\u4f53\u53d8\u91cf\u8f6c\u6362\u6210json\u5b57\u7b26\u4e32\u7684\u5f62\u5f0f<\/strong><\/span><\/h6>\n<p>\u8fd9\u91cc\u5217\u51fa\u4e24\u4e2a\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li>json.NewEncoder(b).Encode(struct_var)<\/li>\n<li>b2, err := json.Marshal(struct_var)<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true \">package main\r\n\r\nimport (\r\n    \"bytes\"\r\n    \"encoding\/json\"\r\n    \"log\"\r\n)\r\n\r\ntype User struct {\r\n    Name    string\r\n    Age     uint32\r\n    Balance uint64\r\n    test    int \/\/ \u5728 struct \u4e2d key \u662f\u5c0f\u5199\u5f00\u5934\u7684\u9879\u4e3a\u300c\u4e0d\u53ef\u5bfc\u51fa\u9879\u300d\uff0c\u56e0\u4e3a\u53ea\u6709\u5927\u5199\u7684\u624d\u662f\u53ef\u5bf9\u5916\u63d0\u4f9b\u8bbf\u95ee\u7684\r\n}\r\n\r\n\/\/ \u5728 tag \u4e2d\u5b9a\u4e49\u7684\u540d\u79f0\u4e0d\u9700\u8981\u9996\u5b57\u6bcd\u5927\u5199\uff0c\u4e14\u5bf9\u5e94\u7684 json \u4e32\u4e2d\u5b57\u6bb5\u540d\u79f0\u4ecd\u7136\u5927\u5c0f\u5199\u4e0d\u654f\u611f\r\ntype Employee struct {\r\n    Name   string `json:\"empname\"` \/\/ tag\u6807\u7b7e\r\n    Number int    `json:\"empid\"`\r\n}\r\n\r\nfunc main() {\r\n    u := User{Name: \"ixyzero.com\", Balance: 8}\r\n    b := new(bytes.Buffer)\r\n    json.NewEncoder(b).Encode(u) \/\/ \u65b9\u6cd5\u4e00\r\n\r\n    log.Printf(\"[u]value: %v\\ntype: %T\\n\\n\", u, u) \/\/ type: main.User \uff08\u53ef\u4ee5\u770b\u5230test\u5b57\u6bb5\u7684\u5185\u5bb9\uff09\r\n    log.Printf(\"[b]value: %v\\ntype: %T\\n\", b, b)   \/\/ type: *bytes.Buffer\r\n\r\n    user := User{Name: \"ixyzero\", Age: 20, Balance: 5, test: 111}\r\n    b2, err := json.Marshal(user) \/\/ \u65b9\u6cd5\u4e8c\r\n    if err != nil {\r\n        log.Println(err)\r\n    }\r\n    log.Printf(\"[b2]value: %v\\ntype: %T\\n\", b2, b2) \/\/ type: []uint8\r\n\r\n    str2 := string(b2)\r\n    log.Printf(\"[str2]value: %v\\ntype: %T\\n\", str2, str2) \/\/ type: string\r\n\r\n    emp := &amp;Employee{Name: \"Rocky\", Number: 5454}\r\n    e, err := json.Marshal(emp)\r\n    if err != nil {\r\n        log.Println(err)\r\n    }\r\n    str2 = string(e)\r\n    log.Printf(\"[str2]value: %v\\ntype: %T\\n\", str2, str2) \/\/ type: string\r\n}\r\n\r\n\/*\r\n2018\/10\/27 14:52:02 [u]value: {ixyzero.com 0 8 0}\r\ntype: main.User\r\n\r\n2018\/10\/27 14:52:02 [b]value: {\"Name\":\"ixyzero.com\",\"Age\":0,\"Balance\":8}\r\n\r\ntype: *bytes.Buffer\r\n2018\/10\/27 14:52:02 [b2]value: [123 34 78 97 109 101 34 58 34 105 120 121 122 101 114 111 34 44 34 65 103 101 34 58 50 48 44 34 66 97 108 97 110 99 101 34 58 53 125]\r\ntype: []uint8\r\n2018\/10\/27 14:52:02 [str2]value: {\"Name\":\"ixyzero\",\"Age\":20,\"Balance\":5}\r\ntype: string\r\n2018\/10\/27 14:52:02 [str2]value: {\"empname\":\"Rocky\",\"empid\":5454}\r\ntype: string\r\n*\/<\/pre>\n<h6><span style=\"color: #ff0000;\"><strong>3.\u5d4c\u5957json\u5b57\u7b26\u4e32\u7684\u89e3\u6790<\/strong><\/span><\/h6>\n<p>\u4e0a\u97622\u79cd\u8bb2\u7684\u5176\u5b9e\u90fd\u662f\u683c\u5f0f\u6bd4\u8f83\u7b80\u5355\u7684json\u5b57\u7b26\u4e32\u7684\u5904\u7406\uff0c\u4f46\u5b9e\u9645\u573a\u666f\u4e2d\u53ef\u80fd\u5d4c\u5957\u683c\u5f0f\u7684json\u5b57\u7b26\u4e32\u4f1a\u5360\u591a\u6570\uff0c\u6bd4\u5982\uff1a<\/p>\n<pre class=\"lang:default decode:true \" title=\"nested.json\">{\r\n  \"args\": {},\r\n  \"data\": \"{\\\"title\\\":\\\"a breakfast\\\"}\",\r\n  \"files\": {},\r\n  \"form\": {},\r\n  \"headers\": {\r\n    \"Accept-Encoding\": \"gzip\",\r\n    \"Connection\": \"close\",\r\n    \"Content-Length\": \"23\",\r\n    \"Content-Type\": \"application\/json\",\r\n    \"Cookie\": \"id=anyone\",\r\n    \"Host\": \"httpbin.org\",\r\n    \"User-Agent\": \"iPhone X\"\r\n  },\r\n  \"json\": {\r\n    \"title\": \"a breakfast\"\r\n  },\r\n  \"origin\": \"137.21.83.79\",\r\n  \"url\": \"https:\/\/httpbin.org\/post\"\r\n}<\/pre>\n<p><span style=\"color: #ff0000;\">\u9488\u5bf9\u8fd9\u79cd\u8f83\u4e3a\u590d\u6742\u7684\u5d4c\u5957\u683c\u5f0fjson\u5b57\u7b26\u4e32\u7684\u5904\u7406\uff0c\u65b9\u6cd5\u7b80\u5355\u7684\u6765\u8bf4\u67093\u79cd\uff1a<\/span><\/p>\n<ul>\n<li>\u4e00\u3001\u5148\u5b9a\u4e49\u597d\uff08\u5bf9\u5e94\u683c\u5f0f\uff09\u5d4c\u5957\u7ed3\u6784\u7684struct\u7ed3\u6784\u4f53\uff1b<\/li>\n<li>\u4e8c\u3001\u5148\u4f7f\u7528 interface{} \u63a5\u6536\uff0c\u5b9e\u9645\u4f7f\u7528\u7684\u65f6\u5019\u518d\u5904\u7406\u4e00\u904d\uff1b<\/li>\n<li>\u4e09\u3001\u76f4\u63a5\u4f7f\u7528\u5916\u90e8\u5305\uff0c\u6bd4\u5982\uff1a<a href=\"https:\/\/github.com\/tidwall\/gjson\">gjson<\/a>\u6216\u8005<a href=\"https:\/\/github.com\/valyala\/fastjson\">fastjson<\/a>\uff1b<\/li>\n<\/ul>\n<p>\u8fd9\u91cc\u4e3a\u4e86\u6f14\u793a\u65b9\u4fbf\u8d77\u89c1\uff0c\u5148\u7528gjson\u8bd5\u8bd5\uff0c\u5176\u4e2dnested.json\u6587\u4ef6\u7684\u5185\u5bb9\u662f\u4e0a\u9762\u7684\u90a3\u6bb5json\u5b57\u7b26\u4e32\u3002<\/p>\n<pre class=\"lang:default decode:true\">package main\r\n\r\nimport (\r\n    \"encoding\/json\"\r\n    \"fmt\"\r\n    \"github.com\/tidwall\/gjson\"\r\n    \"io\/ioutil\"\r\n    \"os\"\r\n)\r\n\r\nfunc typeof(v interface{}) string {\r\n    switch v.(type) {\r\n    case int:\r\n        return \"int\"\r\n    case float64:\r\n        return \"float64\"\r\n    case map[string]interface{}:\r\n        return \"map[string]interface {}\"\r\n    \/\/... etc\r\n    default:\r\n        return \"unknown\"\r\n    }\r\n}\r\n\r\nfunc main() {\r\n\r\n    file, e := ioutil.ReadFile(\".\/nested.json\")\r\n    if e != nil {\r\n        fmt.Printf(\"File error: %v\\n\", e)\r\n        os.Exit(1)\r\n    }\r\n    myJson := string(file)\r\n    m, ok := gjson.Parse(myJson).Value().(map[string]interface{})\r\n    if !ok {\r\n        fmt.Println(\"Error\")\r\n    }\r\n\r\n    for key, item := range m {\r\n        value_type := typeof(item)\r\n        if value_type == \"map[string]interface {}\" {\r\n            fmt.Printf(\"key: %v(%T), item: %v, type: %T\\n\", key, key, item, item)\r\n            itemMap := item.(map[string]interface{}) \/\/ \u8981\u5148\u8fdb\u884c\u4e00\u6b21\u7c7b\u578b\u8f6c\u6362\uff08\u6682\u65f6\u8fd8\u6ca1\u641e\u6e05\u695a\u5177\u4f53\u539f\u56e0\uff09\r\n            fmt.Printf(\"key: %v(%T), itemMap: %v, type: %T\\n\", key, key, itemMap, itemMap)\r\n            \/\/ for k, v := range item { \/\/ cannot range over item (type interface {})\r\n            for k, v := range itemMap {\r\n                fmt.Printf(\"\\tkey: %v(%T), value: %v, type: %T\\n\", k, k, v, v)\r\n            }\r\n        } else {\r\n            fmt.Printf(\"key: %v(%T), value: %v, type: %T\\n\", key, key, item, item)\r\n        }\r\n    }\r\n    fmt.Println()\r\n\r\n    jsonBytes, err := json.Marshal(m)\r\n    if err != nil {\r\n        fmt.Println(err)\r\n    }\r\n    fmt.Println(string(jsonBytes))\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/8270816\/converting-go-struct-to-json\">Go \u8bed\u8a00\u4e2d\u5982\u4f55\u5c06\u4e00\u4e2a struct \u7ed3\u6784\u4f53\u8f6c\u6362\u6210 json \u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff1f<\/a><br \/>\n<a href=\"http:\/\/www.golangprograms.com\/how-to-convert-go-struct-to-json.html\">http:\/\/www.golangprograms.com\/how-to-convert-go-struct-to-json.html<\/a><\/li>\n<li><strong><a href=\"https:\/\/www.cnblogs.com\/liang1101\/p\/6741262.html\">Go_14\uff1aGoLang\u4e2d json\u3001map\u3001struct \u4e4b\u95f4\u7684\u76f8\u4e92\u8f6c\u5316<\/a><\/strong>#<span style=\"color: #ff0000;\"><strong>nice<\/strong><\/span><\/li>\n<li><a href=\"http:\/\/goinbigdata.com\/how-to-correctly-serialize-json-string-in-golang\/\">How To Correctly Serialize JSON String In Golang<\/a><\/li>\n<li><strong><a href=\"https:\/\/stackoverflow.com\/questions\/21268000\/unmarshaling-nested-json-objects-in-golang\">Unmarshaling nested JSON objects in Golang<\/a><\/strong>#nice<\/li>\n<li><a href=\"https:\/\/blog.serverbooter.com\/post\/parsing-nested-json-in-go\/\">Parsing Deeply Nested JSON in Go<\/a><\/li>\n<li><a href=\"http:\/\/www.361way.com\/go-http-json\/5794.html\">golang json\u6570\u636e\u7684\u5904\u7406<\/a><br \/>\n<a href=\"http:\/\/ju.outofmemory.cn\/entry\/366846\">http:\/\/ju.outofmemory.cn\/entry\/366846<\/a><\/li>\n<li><a href=\"http:\/\/songran.net\/2017\/03\/08\/golang_parse_json\/\">golang\u5982\u4f55\u89e3\u6790\u7ed3\u6784\u4e0d\u786e\u5b9a\u7684Json<\/a><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/qq_33204116\/article\/details\/50700705\">golang\u89e3\u6790json\u683c\u5f0f<\/a><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/wangshubo1989\/article\/details\/79193383\">Go\u5b9e\u6218&#8211;Golang Response Snippets: JSON, XML and more(http\u8bf7\u6c42\u8fd4\u56de\u503c)<\/a>#<span style=\"color: #ff0000;\"><strong>nice<\/strong><\/span><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/wangshubo1989\/article\/details\/70245570\">Go\u5b9e\u6218&#8211;net\/http\u4e2dJSON\u7684\u4f7f\u7528(The way to go)<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u73b0\u5728\u7684json\u683c\u5f0f\u7528\u7684\u975e\u5e38\u4e4b\u591a\uff0c\u6bd4\u5982\uff1a\u914d\u7f6e\u6587\u4ef6\uff0c\u63a5\u53e3\u4e4b\u95f4\u7684\u6570\u636e\u4f20\u9012\u3002\u6240\u4ee5\uff0c\u7528Go \u8bed\u8a00 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7,12],"tags":[564,181,416],"class_list":["post-4151","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","category-tools","tag-golang","tag-json","tag-struct"],"views":7066,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=4151"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4151\/revisions"}],"predecessor-version":[{"id":4152,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4151\/revisions\/4152"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}