{"id":299260,"date":"2024-05-20T17:13:20","date_gmt":"2024-05-20T09:13:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/299260.html"},"modified":"2024-05-20T17:13:36","modified_gmt":"2024-05-20T09:13:36","slug":"%e5%9c%a8-go-%e7%bc%96%e7%a8%8b%e4%b8%ad%e5%a6%82%e4%bd%95%e8%a7%a3%e6%9e%90-json-%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/299260.html","title":{"rendered":"\u5728 Go \u7f16\u7a0b\u4e2d\u5982\u4f55\u89e3\u6790 Json \u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27151334\/3bfdfc1c-0f4a-47ce-8d74-53468ae06301.webp\" alt=\"\u5728 Go \u7f16\u7a0b\u4e2d\u5982\u4f55\u89e3\u6790 Json \u6570\u636e\" \/><\/p>\n<p><p>\u89e3\u6790JSON\u6570\u636e\u5728Go\u7f16\u7a0b\u4e2d\u662f\u4e00\u9879\u57fa\u7840\u4e14\u6781\u5176\u91cd\u8981\u7684\u6280\u80fd\u3002\u8981\u6709\u6548\u5730\u89e3\u6790JSON\u6570\u636e\uff0c\u4f60\u9700\u8981\u638c\u63e1\u51e0\u4e2a\u5173\u952e\u6b65\u9aa4\uff1a<strong>\u4f7f\u7528<code>encoding\/json<\/code>\u5305\u3001\u5b9a\u4e49\u9002\u5408JSON\u7ed3\u6784\u7684Go\u7ed3\u6784\u4f53\u3001\u4f7f\u7528<code>json.Unmarshal<\/code>\u8fdb\u884c\u89e3\u6790\u3001\u5229\u7528\u63a5\u53e3<code>interface{}<\/code>\u5904\u7406\u4e0d\u786e\u5b9a\u7684\u6570\u636e\u7ed3\u6784<\/strong>\u3002\u8fd9\u4e9b\u6b65\u9aa4\u80fd\u786e\u4fdd\u4f60\u5728Go\u7a0b\u5e8f\u4e2d\u987a\u5229\u5730\u5904\u7406JSON\u6570\u636e\u3002\u6700\u503c\u5f97\u6df1\u5165\u63a2\u8ba8\u7684\u662f<strong>\u4f7f\u7528<code>encoding\/json<\/code>\u5305<\/strong>\uff0c\u56e0\u4e3a\u5b83\u662f\u6240\u6709JSON\u5904\u7406\u64cd\u4f5c\u7684\u57fa\u7840\u3002\u6b64\u5305\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684\u51fd\u6570\u548c\u7c7b\u578b\uff0c\u65e8\u5728\u5e2e\u52a9\u7a0b\u5e8f\u5458\u8f7b\u677e\u5730\u8fdb\u884cJSON\u7684\u7f16\u7801\u548c\u89e3\u7801\u3002\u7406\u89e3\u5e76\u719f\u7ec3\u4f7f\u7528\u8fd9\u4e2a\u5305\u662f\u89e3\u6790JSON\u6570\u636e\u7684\u9996\u8981\u4efb\u52a1\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u5982\u4f55\u5728Go\u4e2d\u4f7f\u7528\u6b64\u5305\u53ca\u5176\u4ed6\u6b65\u9aa4\u6765\u89e3\u6790JSON\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e00\u3001\u5bfc\u5165<code>ENCODING\/JSON<\/code>\u5305<\/strong><\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u89e3\u6790JSON\u6570\u636e\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165Go\u6807\u51c6\u5e93\u4e2d\u7684<code>encoding\/json<\/code>\u5305\u3002\u8fd9\u4e2a\u5305\u542b\u6709\u7f16\u7801\u548c\u89e3\u7801JSON\u6570\u636e\u6240\u9700\u7684\u6240\u6709\u57fa\u7840\u529f\u80fd\u3002\u5bfc\u5165\u6b64\u5305\u4e4b\u540e\uff0c\u4fbf\u53ef\u4f7f\u7528\u5176\u63d0\u4f9b\u7684<code>Unmarshal<\/code>\u548c<code>Marshal<\/code>\u51fd\u6570\uff0c\u5206\u522b\u7528\u4e8e\u89e3\u6790\u548c\u751f\u6210JSON\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u5bfc\u5165\u8be5\u5305\u53ea\u9700\u5728Go\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\u884c\uff1a<code>import &quot;encoding\/json&quot;<\/code>\u3002\u8fd9\u5141\u8bb8\u7a0b\u5e8f\u8bbf\u95ee<code>json<\/code>\u5305\u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u51fd\u6570\u548c\u7c7b\u578b\uff0c\u4f7f\u5904\u7406JSON\u6570\u636e\u53d8\u5f97\u7b80\u5355\u800c\u76f4\u63a5\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e8c\u3001\u5b9a\u4e49GO\u7ed3\u6784\u4f53<\/strong><\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u80fd\u591f\u6709\u6548\u5730\u89e3\u6790JSON\u6570\u636e\uff0c\u9700\u8981\u5728Go\u4ee3\u7801\u4e2d\u5b9a\u4e49\u548cJSON\u6570\u636e\u7ed3\u6784\u76f8\u5339\u914d\u7684\u7ed3\u6784\u4f53\u3002\u6bcf\u4e2a\u7ed3\u6784\u4f53\u5b57\u6bb5\u53ef\u4ee5\u901a\u8fc7\u6807\u7b7e\u6765\u6307\u5b9a\u5bf9\u5e94\u7684JSON\u952e\u540d\uff0c\u8fd9\u662f\u5229\u7528\u7ed3\u6784\u4f53\u6807\u7b7e\uff08\u901a\u5e38\u662f<code>json:&quot;keyName&quot;<\/code>\uff09\u5b9e\u73b0\u7684\u3002\u8fd9\u4e00\u6b65\u9aa4\u81f3\u5173\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u786e\u4fdd\u4e86JSON\u6570\u636e\u80fd\u591f\u88ab\u6b63\u786e\u5730\u6620\u5c04\u5230Go\u7ed3\u6784\u4f53\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4ed4\u7ec6\u5206\u6790JSON\u6570\u636e\u7684\u7ed3\u6784\uff0c\u7136\u540e\u521b\u5efa\u76f8\u5e94\u7684Go\u7ed3\u6784\u4f53\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5305\u542b\u59d3\u540d\u548c\u5e74\u9f84\u7684JSON\u6570\u636e\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49\u5982\u4e0b\u7ed3\u6784\u4f53\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-go\">type Person struct {<\/p>\n<p>    Name string `json:&quot;name&quot;`<\/p>\n<p>    Age  int    `json:&quot;age&quot;`<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6ce8\u610f\uff0c\u7ed3\u6784\u4f53\u4e2d\u7684\u5b57\u6bb5\u540d\u79f0\u5e94\u8be5\u4e0eJSON\u952e\u540d\u76f8\u5339\u914d\uff0c\u6216\u901a\u8fc7\u6b63\u786e\u7684\u6807\u7b7e\u6765\u6620\u5c04\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e09\u3001\u4f7f\u7528<code>JSON.UNMARSHAL<\/code>\u8fdb\u884c\u89e3\u6790<\/strong><\/h3>\n<\/p>\n<p><p>\u62e5\u6709\u4e86\u4e0eJSON\u6570\u636e\u76f8\u5339\u914d\u7684Go\u7ed3\u6784\u4f53\u540e\uff0c\u4e0b\u4e00\u6b65\u5c31\u662f\u5b9e\u9645\u8fdb\u884c\u89e3\u6790\u3002\u8fd9\u662f\u901a\u8fc7<code>json.Unmarshal<\/code>\u51fd\u6570\u5b8c\u6210\u7684\uff0c\u5b83\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1aJSON\u6570\u636e\u7684\u5b57\u8282\u5207\u7247\u548c\u8981\u89e3\u6790\u5230\u7684\u76ee\u6807\u7ed3\u6784\u4f53\u7684\u6307\u9488\u3002<\/p>\n<\/p>\n<p><p>\u5904\u7406JSON\u89e3\u6790\u901a\u5e38\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5c06JSON\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u8282\u5207\u7247\u3002<\/li>\n<li>\u8c03\u7528<code>json.Unmarshal<\/code>\u51fd\u6570\uff0c\u4f20\u5165JSON\u5b57\u8282\u5207\u7247\u548c\u76ee\u6807\u7ed3\u6784\u4f53\u7684\u6307\u9488\u3002<\/li>\n<\/ol>\n<p><p>\u4f8b\u5982\uff0c\u89e3\u6790\u4e4b\u524d\u7684Person\u7ed3\u6784\u4f53\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-go\">var person Person<\/p>\n<p>jsonData := `{&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30}`<\/p>\n<p>err := json.Unmarshal([]byte(jsonData), &amp;person)<\/p>\n<p>if err != nil {<\/p>\n<p>    log.Fatalf(&quot;JSON Unmarshaling f<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led: %s&quot;, err)<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5f53<code>json.Unmarshal<\/code>\u88ab\u8c03\u7528\u65f6\uff0c\u5b83\u5c06\u89e3\u6790\u5b57\u8282\u5207\u7247\u4e2d\u7684JSON\u6570\u636e\uff0c\u5e76\u5c06\u89e3\u6790\u7ed3\u679c\u586b\u5145\u5230\u4f20\u5165\u7684\u7ed3\u6784\u4f53\u4e2d\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u662f\u81ea\u52a8\u7684\uff0c\u5b83\u4e25\u683c\u4f9d\u7167\u7ed3\u6784\u4f53\u6807\u7b7e\u4e0eJSON\u952e\u7684\u6620\u5c04\u8fdb\u884c\u3002<\/p>\n<\/p>\n<p><h3><strong>\u56db\u3001\u5904\u7406\u4e0d\u786e\u5b9a\u7684\u6570\u636e\u7ed3\u6784<\/strong><\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6709\u65f6\u5019\u6211\u4eec\u65e0\u6cd5\u4e8b\u5148\u77e5\u9053JSON\u6570\u636e\u7684\u7ed3\u6784\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u5229\u7528\u7a7a\u63a5\u53e3<code>interface{}<\/code>\u6765\u5904\u7406\u3002\u7a7a\u63a5\u53e3\u53ef\u4ee5\u63a5\u53d7\u4efb\u4f55\u7c7b\u578b\u7684\u6570\u636e\uff0c\u8fd9\u4f7f\u5f97\u5b83\u6210\u4e3a\u5904\u7406\u4e0d\u786e\u5b9a\u7ed3\u6784JSON\u6570\u636e\u7684\u7406\u60f3\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><p>\u89e3\u6790\u672a\u77e5\u7ed3\u6784\u7684JSON\uff0c\u9996\u5148\u9700\u8981\u4f7f\u7528<code>interface{}<\/code>\u6765\u63a5\u6536\u89e3\u6790\u7684\u6570\u636e\u3002\u4e4b\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u7c7b\u578b\u65ad\u8a00\u6216\u7c7b\u578b\u5224\u65ad\u6765\u8bbf\u95ee\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-go\">var result interface{}<\/p>\n<p>jsonData := `{&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30}`<\/p>\n<p>json.Unmarshal([]byte(jsonData), &amp;result)<\/p>\n<p>dataMap := result.(map[string]interface{})<\/p>\n<p>for key, value := range dataMap {<\/p>\n<p>    switch v := value.(type) {<\/p>\n<p>    case string:<\/p>\n<p>        fmt.Println(key, &quot;is string&quot;, v)<\/p>\n<p>    case int:<\/p>\n<p>        fmt.Println(key, &quot;is int&quot;, v)<\/p>\n<p>    case float64:<\/p>\n<p>        fmt.Println(key, &quot;is float64&quot;, v)<\/p>\n<p>    default:<\/p>\n<p>        fmt.Println(key, &quot;is of a type I don&#039;t know how to handle&quot;)<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u5c06<code>result<\/code>\u53d8\u91cf\u58f0\u660e\u4e3a<code>interface{}<\/code>\u7c7b\u578b\uff0c<code>json.Unmarshal<\/code>\u53ef\u4ee5\u5c06\u4efb\u4f55\u7ed3\u6784\u7684JSON\u6570\u636e\u89e3\u6790\u5230<code>result<\/code>\u4e2d\u3002\u7531\u4e8e<code>result<\/code>\u662f\u7a7a\u63a5\u53e3\uff0cJSON\u4e2d\u7684\u6570\u636e\u6700\u7ec8\u4f1a\u88ab\u89e3\u6790\u4e3aGo\u4e2d\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff08\u5982map\u3001slice\u3001string\u3001float64\u7b49\uff09\u3002\u56e0\u6b64\uff0c\u5728\u5904\u7406\u5177\u4f53\u7684\u6570\u636e\u65f6\uff0c\u9700\u8981\u6839\u636e\u5176\u5b9e\u9645\u7c7b\u578b\u8fdb\u884c\u7c7b\u578b\u65ad\u8a00\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u5373\u53ef\u5728Go\u7a0b\u5e8f\u4e2d\u7075\u6d3b\u5730\u89e3\u6790JSON\u6570\u636e\uff0c\u65e0\u8bba\u5176\u7ed3\u6784\u662f\u5df2\u77e5\u8fd8\u662f\u672a\u77e5\u3002\u638c\u63e1\u8fd9\u4e9b\u6280\u80fd\u662f\u6bcf\u4e2aGo\u5f00\u53d1\u8005\u7684\u57fa\u672c\u8981\u6c42\uff0c\u4e5f\u662f\u5b9e\u9645\u9879\u76ee\u5f00\u53d1\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u6280\u672f\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q: \u6211\u8be5\u5982\u4f55\u5728Go\u7f16\u7a0b\u4e2d\u89e3\u6790JSON\u6570\u636e\uff1f<\/strong><\/p>\n<p>A: \u5728Go\u7f16\u7a0b\u4e2d\uff0c\u89e3\u6790JSON\u6570\u636e\u53ef\u4ee5\u4f7f\u7528<code>encoding\/json<\/code>\u5305\u63d0\u4f9b\u7684\u51fd\u6570\u548c\u7c7b\u578b\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u7ed3\u6784\u4f53\u6765\u5b58\u50a8\u4f60\u8981\u89e3\u6790\u7684JSON\u6570\u636e\u7684\u5b57\u6bb5\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>json.Unmarshal()<\/code>\u51fd\u6570\u5c06JSON\u6570\u636e\u89e3\u6790\u5230\u4f60\u5b9a\u4e49\u7684\u7ed3\u6784\u4f53\u4e2d\u3002\u6700\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee\u7ed3\u6784\u4f53\u4e2d\u7684\u5b57\u6bb5\u6765\u83b7\u53d6\u89e3\u6790\u540e\u7684\u6570\u636e\u4e86\u3002<\/p>\n<p><strong>Q: Go\u7f16\u7a0b\u4e2d\u6709\u6ca1\u6709\u66f4\u5feb\u901f\u7684\u89e3\u6790JSON\u6570\u636e\u7684\u65b9\u6cd5\uff1f<\/strong><\/p>\n<p>A: \u662f\u7684\uff0cGo\u7f16\u7a0b\u4e2d\u53ef\u4ee5\u4f7f\u7528<code>jsoniter<\/code>\u5305\u6765\u66f4\u5feb\u901f\u5730\u89e3\u6790JSON\u6570\u636e\u3002<code>jsoniter<\/code>\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684JSON\u89e3\u6790\u5e93\uff0c\u53ef\u4ee5\u6bd4\u6807\u51c6\u5e93<code>encoding\/json<\/code>\u66f4\u5feb\u5730\u5904\u7406JSON\u6570\u636e\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>jsoniter.Unmarshal()<\/code>\u51fd\u6570\u6765\u89e3\u6790JSON\u6570\u636e\uff0c\u5176\u7528\u6cd5\u4e0e\u6807\u51c6\u5e93\u7c7b\u4f3c\u3002\u6ce8\u610f\uff0c\u4f60\u9700\u8981\u5148\u4f7f\u7528<code>jsoniter.ConfigCompatibleWithStandardLibrary<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u914d\u7f6e\u5bf9\u8c61\u3002<\/p>\n<p><strong>Q: \u9664\u4e86\u89e3\u6790JSON\u6570\u636e\uff0cGo\u7f16\u7a0b\u4e2d\u8fd8\u6709\u54ea\u4e9b\u4e0eJSON\u76f8\u5173\u7684\u64cd\u4f5c\uff1f<\/strong><\/p>\n<p>A: \u9664\u4e86\u89e3\u6790JSON\u6570\u636e\uff0cGo\u7f16\u7a0b\u4e2d\u8fd8\u6709\u8bb8\u591a\u4e0eJSON\u76f8\u5173\u7684\u64cd\u4f5c\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>json.Marshal()<\/code>\u51fd\u6570\u5c06Go\u7684\u6570\u636e\u7ed3\u6784\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u7684\u6570\u636e\u3002\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>json.NewEncoder()<\/code>\u548c<code>json.NewDecoder()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u5bf9\u5e94\u7684\u7f16\u7801\u5668\u548c\u89e3\u7801\u5668\uff0c\u8fdb\u884cJSON\u7684\u7f16\u7801\u548c\u89e3\u7801\u64cd\u4f5c\u3002\u6b64\u5916\uff0cGo\u8fd8\u63d0\u4f9b\u4e86<code>json.Valid()<\/code>\u51fd\u6570\u6765\u9a8c\u8bc1\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u662f\u5408\u6cd5\u7684JSON\u683c\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u89e3\u6790JSON\u6570\u636e\u5728Go\u7f16\u7a0b\u4e2d\u662f\u4e00\u9879\u57fa\u7840\u4e14\u6781\u5176\u91cd\u8981\u7684\u6280\u80fd\u3002\u8981\u6709\u6548\u5730\u89e3\u6790JSON\u6570\u636e\uff0c\u4f60\u9700\u8981\u638c\u63e1\u51e0\u4e2a\u5173\u952e\u6b65\u9aa4\uff1a\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":299283,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/299260"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=299260"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/299260\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/299283"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=299260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=299260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=299260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}