{"id":41891,"date":"2024-12-01T10:11:58","date_gmt":"2024-12-01T02:11:58","guid":{"rendered":"https:\/\/fwq.ai\/blog\/41891\/"},"modified":"2024-12-01T10:11:58","modified_gmt":"2024-12-01T02:11:58","slug":"golang%e8%a7%a3%e7%bb%84json%e5%93%8d%e5%ba%94%ef%bc%8c%e7%84%b6%e5%90%8e%e5%b0%86%e5%ad%97%e6%ae%b5%e5%90%8d%e7%a7%b0%e8%bd%ac%e6%8d%a2%e4%b8%basnake_case","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/41891\/","title":{"rendered":"Golang\u89e3\u7ec4JSON\u54cd\u5e94\uff0c\u7136\u540e\u5c06\u5b57\u6bb5\u540d\u79f0\u8f6c\u6362\u4e3asnake_case"},"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\u89e3\u7ec4JSON\u54cd\u5e94\uff0c\u7136\u540e\u5c06\u5b57\u6bb5\u540d\u79f0\u8f6c\u6362\u4e3asnake_case<\/span><\/p>\n<h1>Golang\u89e3\u7ec4JSON\u54cd\u5e94\uff0c\u7136\u540e\u5c06\u5b57\u6bb5\u540d\u79f0\u8f6c\u6362\u4e3asnake_case<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-21 20:15: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>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGolang\u89e3\u7ec4JSON\u54cd\u5e94\uff0c\u7136\u540e\u5c06\u5b57\u6bb5\u540d\u79f0\u8f6c\u6362\u4e3asnake_case\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u60f3\u83b7\u53d6\u5e26\u6709 json \u6807\u7b7e\u7684\u6570\u636e\uff0c\u5176\u6e90\u4e3a pascalcase \u683c\u5f0f\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u5230\u6211\u7684\u6570\u636e\u5e93\u4e2d\u3002\u4f46\u5728\u8fdb\u5165\u6570\u636e\u5e93\u4e4b\u524d\uff0c\u6211\u60f3\u5c06pascalcase\u683c\u5f0f\u66f4\u6539\u4e3asnake_case\u683c\u5f0f\u3002<\/p>\n<p>\u6211\u7684\u95ee\u9898\u4f3c\u4e4e\u4e0e\u8fd9\u4e2a\u95ee\u9898\u76f8\u53cd\uff08golang unmarshal an json response, then marshal with struct \u5b57\u6bb5\u540d\u79f0\uff09\u3002\u4f46\u6211\u4e0d\u60f3\u5728\u540d\u79f0\u5b57\u6bb5\u4e2d\u4f7f\u7528 pascalcase\uff0c\u800c\u662f\u4f7f\u7528 snake_case<\/p>\n<p>\u8fd9\u662f\u6211\u7f16\u5199\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"encoding\/json\"\n    \"log\"\n)\n\n\/\/ models data to save in db\ntype (\n    person struct {\n        firstname string      `json:\"firstname\"`\n        lastname  string      `json:\"lastname\"`\n        children  []childdata `json:\"children,omitempty\"`\n    }\n    childdata struct {\n        childname string `json:\"childname\"`\n        age       int    `json:\"age\"`\n        favcolor  string `json:\"favcolor\"`\n    }\n    personout struct {\n        firstname string      `json:\"first_name\"`\n        lastname  string      `json:\"last_name\"`\n        children  []childdata `json:\"children,omitempty\"`\n    }\n    childdataout struct {\n        childname string `json:\"child_name\"`\n        age       int    `json:\"age\"`\n        favcolor  string `json:\"fav_color\"`\n    }\n)\n\n\/\/ grisha is data from fetch api\nvar grisha = map[string]interface{}{\n    \"firstname\": \"grisha\",\n    \"lastname\":  \"jeager\",\n    \"children\": []map[string]interface{}{\n        {\n            \"childname\": \"zeke\",\n            \"age\":       2,\n            \"favcolor\":  \"blue\",\n        }, {\n            \"childname\": \"eren\",\n            \"age\":       3,\n            \"favcolor\":  \"red\",\n        },\n    },\n}\n\nfunc getjsonfromapi(data map[string]interface{}) []byte {\n    grishajson, _ := json.marshalindent(grisha, \"\", \"    \")\n    return grishajson\n}\n\nfunc parsingjson(jsoninput []byte) {\n    var person person\n    json.unmarshal(jsoninput, &amp;person)\n\n    out := personout(person)\n    payload2, err := json.marshalindent(out, \"\", \"    \")\n    if err != nil {\n        panic(err)\n    }\n    log.println(string(payload2))\n}\n\nfunc main() {\n    data := getjsonfromapi(grisha)\n    parsingjson(data)\n}<\/pre>\n<p>\u6211\u60f3\u8981\u7684\u7ed3\u679c\u662f\u8fd9\u6837\u7684<\/p>\n<pre>{\n    \"first_name\": \"grisha\",\n    \"last_name\": \"jeager\",\n    \"children\": [\n        {\n            \"child_name\": \"zeke\",\n            \"age\": 2,\n            \"fav_color\": \"blue\"\n        },\n        {\n            \"child_name\": \"eren\",\n            \"age\": 3,\n            \"fav_color\": \"red\"\n        }\n    ]\n}<\/pre>\n<p>\u4f46\u7ed3\u679c\u5728\u5d4c\u5957\u5b57\u6bb5\u4e2d\u4ecd\u7136\u5305\u542b pascalcase\uff1a<\/p>\n<pre>{\n    \"first_name\": \"Grisha\",\n    \"last_name\": \"Jeager\",\n    \"children\": [\n        {\n            \"ChildName\": \"Zeke\",\n            \"Age\": 2,\n            \"FavColor\": \"blue\"\n        },\n        {\n            \"ChildName\": \"Eren\",\n            \"Age\": 3,\n            \"FavColor\": \"red\"\n        }\n    ]\n}<\/pre>\n<p>\u6211\u60f3\u77e5\u9053\u5982\u4f55\u4ece unmarshal json \u8f6c\u6362\u5d4c\u5957\u7ed3\u6784\u7684\u5b57\u6bb5\u540d\u79f0\u3002<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u53ea\u6709\u5f53\u5b83\u4eec\u7684\u57fa\u7840\u7c7b\u578b\uff08\u5ffd\u7565\u6807\u7b7e\uff09\u76f8\u540c\u65f6\uff0c\u5c06\u4e00\u4e2a\u7ed3\u6784\u8f6c\u6362\u4e3a\u53e6\u4e00\u4e2a\u7ed3\u6784\u624d\u6709\u6548\u3002\u5982\u679c\u57fa\u7840\u7c7b\u578b\u4e0d\u76f8\u540c\uff0c\u5219\u65e0\u6cd5\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<p>\u4ee5\u7ed3\u6784\u4f53 <code>s1<\/code> \u548c <code>s2<\/code> \u4e3a\u4f8b\uff0c\u5728\u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u4e2d\uff0c\u5b83\u4eec\u7684<em>\u5e95\u5c42\u7c7b\u578b<\/em>\u662f\u76f8\u540c\u7684\uff0c\u60a8\u53ef\u4ee5\u5c06\u5176\u4e2d\u4e00\u79cd\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\uff1a<\/p>\n<pre>type s1 struct {\n    field t1\n}\ntype s2 struct {\n    field t1\n}\ntype t1 string\n\n_ = s2(s1{}) \/\/ ok\n<\/pre>\n<p>\u4f46\u662f\uff0c\u5728\u4e0b\u4e00\u4e2a\u4ee3\u7801\u7247\u6bb5\u4e2d\uff0c\u5b83\u4eec\u7684<em>\u5e95\u5c42\u7c7b\u578b<\/em>\u5e76\u4e0d\u76f8\u540c\uff0c\u56e0\u6b64\u60a8\u65e0\u6cd5\u5c06\u5176\u4e2d\u4e00\u4e2a\u8f6c\u6362\u4e3a\u53e6\u4e00\u4e2a\uff1a<\/p>\n<pre>type s1 struct {\n    field t1\n}\ntype s2 struct {\n    field t2\n}\ntype t1 string\ntype t2 string\n\n_ = s2(s1{}) \/\/ cannot convert s1{} (value of type s1) to type s2\n<\/pre>\n<p>\u6709\u5173\u66f4\u591a\u8be6\u7ec6\u4fe1\u606f\uff0c\u8bf7\u9605\u8bfb\u6b64\u5904\u7684\u8f6c\u6362\u89c4\u8303\uff1a<\/p>\n<p>\u56e0\u6b64\uff0c\u8981\u5728<em>\u5e95\u5c42\u7c7b\u578b<\/em>\u4e0d\u76f8\u540c\u7684\u4e24\u4e2a\u7ed3\u6784\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\uff0c\u60a8\u5fc5\u987b\u624b\u52a8\u9010\u4e2a\u5b57\u6bb5\u8fdb\u884c\u8f6c\u6362\u3002\u4f46\u662f\uff0c\u5728 json \u5c01\u9001\u5904\u7406\u7684\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0 <code>json.marshaler<\/code> \u63a5\u53e3\u5e76\u4f7f\u5404\u4e2a\u5b9e\u4f8b\u201c\u81ea\u884c\u8f6c\u6362\u201d\u6765\u4f7f\u5176\u53d8\u5f97\u66f4\u597d\u3002<\/p>\n<pre>type person struct {\n    firstname string      `json:\"firstname\"`\n    lastname  string      `json:\"lastname\"`\n    children  []childdata `json:\"children\"`\n}\n\nfunc (p person) marshaljson() ([]byte, error) {\n    type t struct {\n        firstname string      `json:\"first_name\"`\n        lastname  string      `json:\"last_name\"`\n        children  []childdata `json:\"children\"`\n    }\n\n    return json.marshal(t(p))\n}\n\ntype childdata struct {\n    childname string `json:\"childname\"`\n    age       int    `json:\"age\"`\n    favcolor  string `json:\"favcolor\"`\n}\n\nfunc (d childdata) marshaljson() ([]byte, error) {\n    type t struct {\n        childname string `json:\"child_name\"`\n        age       int    `json:\"age\"`\n        favcolor  string `json:\"fav_color\"`\n    }\n\n    return json.marshal(t(d))\n}\n<\/pre>\n<\/p>\n<p>\u6211\u4eec\u9700\u8981\uff1a<\/p>\n<ol>\n<li>\u5c06 person \u6620\u5c04\u5230 personout<\/li>\n<li>\u5c06 childdata \u6620\u5c04\u5230 childdataout<\/li>\n<\/ol>\n<pre>func childDataToChildDataOut(childData ChildData) ChildDataOut {\n    return ChildDataOut{\n        ChildName: childData.ChildName,\n        Age:       childData.Age,\n        FavColor:  childData.FavColor,\n    }\n}\n\nfunc personToPersonOut(person Person) PersonOut {\n    var children []ChildDataOut\n\n    for _, childData := range person.Children {\n        childDataOut := childDataToChildDataOut(childData)\n        children = append(children, childDataOut)\n    }\n\n    return PersonOut{\n        FirstName: person.FirstName,\n        LastName:  person.LastName,\n        Children:  children,\n    }\n}\n<\/pre>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aGolang\u89e3\u7ec4JSON\u54cd\u5e94\uff0c\u7136\u540e\u5c06\u5b57\u6bb5\u540d\u79f0\u8f6c\u6362\u4e3asnake_case\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\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; Golang\u89e3\u7ec4JSON\u54cd\u5e94\uff0c\u7136\u540e\u5c06\u5b57\u6bb5\u540d\u79f0\u8f6c\u6362\u4e3asnake_case Golang\u89e3\u7ec4JSON\u54cd\u5e94\uff0c\u7136\u540e\u5c06\u5b57\u6bb5\u540d\u79f0\u8f6c\u6362\u4e3asnake_case \u6765\u6e90\uff1astackoverflow 2024-04-21 20:15:33 0\u6d4f\u89c8 \u6536\u85cf \u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\u300aGolang\u89e3\u7ec4JSON\u54cd\u5e94\uff0c\u7136\u540e\u5c06\u5b57\u6bb5\u540d\u79f0\u8f6c\u6362\u4e3asnake_case\u300b\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01 \u95ee\u9898\u5185\u5bb9 \u6211\u60f3\u83b7\u53d6\u5e26\u6709 json \u6807\u7b7e\u7684\u6570\u636e\uff0c\u5176\u6e90\u4e3a pascalcase \u683c\u5f0f\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u5230\u6211\u7684\u6570\u636e\u5e93\u4e2d\u3002\u4f46\u5728\u8fdb\u5165\u6570\u636e\u5e93\u4e4b\u524d\uff0c\u6211\u60f3\u5c06pascalcase\u683c\u5f0f\u66f4\u6539\u4e3asnake_case\u683c\u5f0f\u3002 \u6211\u7684\u95ee\u9898\u4f3c\u4e4e\u4e0e\u8fd9\u4e2a\u95ee\u9898\u76f8\u53cd\uff08golang unmarshal an json response, then marshal with struct \u5b57\u6bb5\u540d\u79f0\uff09\u3002\u4f46\u6211\u4e0d\u60f3\u5728\u540d\u79f0\u5b57\u6bb5\u4e2d\u4f7f\u7528 pascalcase\uff0c\u800c\u662f\u4f7f\u7528 snake_case \u8fd9\u662f\u6211\u7f16\u5199\u7684\u793a\u4f8b\u4ee3\u7801\uff1a package main import ( &#8220;encoding\/json&#8221; &#8220;log&#8221; ) \/\/ models data to save in db type ( person struct { firstname string `json:&#8221;firstname&#8221;` lastname [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-41891","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41891","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=41891"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41891\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=41891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=41891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=41891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}