[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-th-1-1-all-go-json-all--*":3,"academy-blog-translations-y5bzges8suonb7n":85},{"data":4,"page":72,"perPage":72,"totalItems":72,"totalPages":72},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":80,"keywords":81,"locale":54,"published_at":82,"scheduled_at":13,"school_blog":76,"short_description":83,"status":74,"title":6,"updated":84,"updated_by":13,"slug":77,"views":79},"EP.13 Go กับ JSON - แปลงข้อมูลให้เข้าใจง่าย!","sclblg987654321","school_blog_translations","\u003Cp>\u003Cspan style=\"font-size:20px;\">\u003Cstrong>Go กับ JSON - แปลงข้อมูลให้เข้าใจง่าย!\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fp>\u003Cp>อยากให้ข้อมูลในโปรแกรมดูง่ายและใช้งานร่วมกับแอปอื่นได้ไหม? JSON คือคำตอบ! วันนี้เราจะมาสอนการแปลงข้อมูลเป็น JSON และแปลง JSON กลับมาใช้ใน Go ได้ง่ายๆ\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>JSON คืออะไร?\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>JSON (JavaScript Object Notation) เป็นรูปแบบข้อมูลที่มีโครงสร้างอ่านง่าย ใช้สื่อสารและแลกเปลี่ยนข้อมูลระหว่างโปรแกรมต่างๆ ได้เป็นอย่างดี ซึ่งนิยมใช้ในแอปพลิเคชันที่ต้องเชื่อมต่อกับ API หรือเซิร์ฟเวอร์\u003C\u002Fp>\u003Cp>\u003Cstrong>โครงสร้างของ JSON\u003C\u002Fstrong>\u003Cbr>JSON เก็บข้อมูลในรูปแบบ คู่ของ \"ชื่อ\": ค่า เช่น:\u003C\u002Fp>\u003Cp>ในตัวอย่างนี้:\u003Cbr>\"name\": \"Alice\" เป็นข้อความ (string)\u003Cbr>\"age\": 25 เป็นตัวเลข (number)\u003Cbr>\"isStudent\": true เป็นค่าจริง\u002Fเท็จ (boolean)\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">{\r\n  \"name\": \"Alice\",\r\n  \"age\": 25,\r\n  \"isStudent\": true\r\n}\r\n\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>การแปลงข้อมูลเป็น JSON ใน Go (Encoding)\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>การแปลงข้อมูลใน Go เป็น JSON ทำได้ง่ายๆ ด้วยคำสั่ง json.Marshal โดยใช้ struct เพื่อเก็บข้อมูลที่เราต้องการแปลง\u003Cbr>ตัวอย่าง:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\r\n\r\nimport (\r\n    \"encoding\u002Fjson\"\r\n    \"fmt\"\r\n)\r\n\r\ntype Person struct {\r\n    Name string `json:\"name\"`\r\n    Age  int    `json:\"age\"`\r\n}\r\n\r\nfunc main() {\r\n    p := Person{Name: \"Alice\", Age: 25}\r\n    jsonData, err := json.Marshal(p)\r\n    if err != nil {\r\n        fmt.Println(\"Error encoding JSON:\", err)\r\n        return\r\n    }\r\n    fmt.Println(string(jsonData)) \u002F\u002F แสดงผล JSON เป็น string\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>ในตัวอย่าง 2\u002F6:\u003Cbr>เราสร้าง struct ชื่อ Person ที่มีฟิลด์ Name และ Age\u003Cbr>ใช้ json.Marshal(p) เพื่อแปลง Person เป็น JSON\u003Cbr>ผลลัพธ์จะได้:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">{\"name\":\"Alice\",\"age\":25}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>การแปลง JSON กลับเป็นข้อมูลใน Go (Decoding)\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>เมื่อเราได้ JSON มาและต้องการแปลงกลับเป็นข้อมูลแบบ struct เราใช้ json.Unmarshal\u003C\u002Fp>\u003Cp>ในตัวอย่างนี้: เราแปลง JSON (jsonData) กลับเป็น struct ชื่อ Person ด้วย json.Unmarshal\u003Cbr>ผลลัพธ์จะแสดงชื่อและอายุของ Person\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\r\n\r\nimport (\r\n    \"encoding\u002Fjson\"\r\n    \"fmt\"\r\n)\r\n\r\ntype Person struct {\r\n    Name string `json:\"name\"`\r\n    Age  int    `json:\"age\"`\r\n}\r\n\r\nfunc main() {\r\n    jsonData := []byte(`{\"name\":\"Alice\",\"age\":25}`)\r\n    var p Person\r\n    err := json.Unmarshal(jsonData, &amp;p)\r\n    if err != nil {\r\n        fmt.Println(\"Error decoding JSON:\", err)\r\n        return\r\n    }\r\n    fmt.Println(p.Name, \"is\", p.Age, \"years old.\")\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>สรุป\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>ใช้ json.Marshal เพื่อแปลงข้อมูลเป็น JSON (Encoding)\u003Cbr>ใช้ json.Unmarshal เพื่อแปลง JSON กลับเป็นข้อมูล (Decoding)\u003Cbr>JSON ช่วยให้ข้อมูลดูง่ายและสามารถใช้งานกับแอปพลิเคชันอื่นได้สะดวก\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>สามารถลองสร้าง struct ที่มีฟิลด์หลายแบบ เช่น ชื่อ อายุ และสถานะการเป็นนักเรียน แล้วแปลงเป็น JSON และแปลงกลับเป็น struct เพื่อดูว่าได้ข้อมูลเหมือนเดิมหรือไม่\u003C\u002Fp>","27_11zon_kishngb04a.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Fesb6txgyufeq1l0\u002F27_11zon_kishngb04a.webp","2026-03-04 08:51:49.815Z","",{"keywords":15,"locale":48,"school_blog":58},[16,23,28,33,38,43],{"collectionId":17,"collectionName":18,"created":19,"created_by":13,"id":20,"name":21,"updated":22,"updated_by":13},"sclkey987654321","school_keywords","2026-03-04 08:26:56.612Z","yf74wkqyamfc5qx","โปรแกรมเมอร์","2026-04-10 16:07:36.426Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:32:39.381Z","da4jbdywyfmkv17","การจัดการข้อมูล","2026-04-10 16:07:58.154Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:51:48.853Z","rl3x8ognoa2hzrb","Unmarshal","2026-04-10 16:14:40.365Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:51:49.059Z","4mugqcb1gl9joc6","Marshal","2026-04-10 16:14:40.504Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:46:24.109Z","o8xfgwdh6k03hxd","JSON","2026-04-10 16:13:13.679Z",{"collectionId":17,"collectionName":18,"created":44,"created_by":13,"id":45,"name":46,"updated":47,"updated_by":13},"2026-03-04 08:20:11.547Z","ey3puyme01a9bsw","Go","2026-04-10 16:07:25.893Z",{"code":49,"collectionId":50,"collectionName":51,"created":52,"flag":53,"id":54,"is_default":55,"label":56,"updated":57},"th","pbc_1989393366","locales","2026-01-22 10:59:55.832Z","twemoji:flag-thailand","s8wri3bt4vgg2ji",true,"Thai","2026-04-10 15:42:46.614Z",{"category":59,"collectionId":60,"collectionName":61,"created":13,"expand":62,"id":76,"slug":77,"updated":78,"views":79},"wqxt7ag2gn7xcmk","pbc_2105096300","school_blogs",{"category":63},{"blogIds":64,"collectionId":65,"collectionName":66,"created":67,"created_by":13,"id":59,"image":68,"image_alt":13,"image_path":69,"label":70,"name":71,"priority":72,"publish_at":73,"scheduled_at":13,"status":74,"updated":75,"updated_by":13},[],"sclcatblg987654321","school_category_blogs","2026-03-04 08:33:53.210Z","59ty92ns80w_15oc1implw.png","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclcatblg987654321\u002Fwqxt7ag2gn7xcmk\u002F59ty92ns80w_15oc1implw.png",{"en":71,"th":71},"Golang The Series",1,"2026-03-16 04:39:38.440Z","published","2026-04-25 02:32:15.470Z","y5bzges8suonb7n","go-json","2026-05-07 18:28:15.754Z",329,"esb6txgyufeq1l0",[20,25,30,35,40,45],"2025-01-30 06:01:31.931Z","เรียนรู้วิธีการแปลงข้อมูลเป็น JSON ด้วย Marshal และการแปลง JSON กลับเป็นข้อมูลด้วย Unmarshal ในภาษา Go","2026-04-22 07:11:50.362Z",{"th":77,"en":77}]