[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-th-1-1-all-go-json-array-all--*":3,"academy-blog-translations-d8h5wqgiutl8ne0":84},{"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":78,"keywords":79,"locale":54,"published_at":80,"scheduled_at":13,"school_blog":76,"short_description":81,"slug":82,"status":74,"title":6,"updated":83,"updated_by":13,"views":77},"EP.13.1 Go กับ JSON Array - จัดการข้อมูลหลายรายการได้ง่ายๆ!","sclblg987654321","school_blog_translations","\u003Cp>\u003Cspan style=\"font-size:20px;\">\u003Cstrong>Go กับ JSON Array - จัดการข้อมูลหลายรายการได้ง่ายๆ!\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fp>\u003Cp>รู้ไหมว่าเราสามารถเก็บข้อมูลหลายๆ รายการใน JSON แบบ Array ได้? เช่น ข้อมูลคนหลายคน หรือรายการสินค้าหลายชิ้น ใน EP นี้ เราจะสอนการใช้ JSON Array ใน Go ตั้งแต่การสร้าง Array แปลงเป็น JSON ไปจนถึงการแปลง JSON Array กลับมาใช้ในโค้ดได้ง่ายๆ!\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>ตัวอย่าง JSON ที่เป็น Array\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>สมมติว่าเรามีข้อมูลเกี่ยวกับกลุ่มบุคคลหลายคน ใน JSON เราสามารถใช้ Array เพื่อเก็บข้อมูลบุคคลหลายคนได้ เช่น:\u003Cbr>ในโค้ดนี้ เราจะแปลง struct ที่เป็น Array ใน Go ให้เป็น JSON และแปลง JSON Array กลับมาเป็น struct ใน Go\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">[\r\n    {\"name\": \"Alice\", \"age\": 25},\r\n    {\"name\": \"Bob\", \"age\": 30},\r\n    {\"name\": \"Charlie\", \"age\": 35}\r\n]\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>การแปลง Array ของ Struct เป็น JSON (Encoding)\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>ใน Go เราใช้ []struct เพื่อสร้าง Array ของ Struct และใช้ json.Marshal เพื่อแปลงเป็น JSON\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    people := []Person{\r\n        {Name: \"Alice\", Age: 25},\r\n        {Name: \"Bob\", Age: 30},\r\n        {Name: \"Charlie\", Age: 35},\r\n    }\r\n\r\n    jsonData, err := json.Marshal(people)\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>ผลลัพธ์:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">[\r\n    {\"name\":\"Alice\",\"age\":25},\r\n    {\"name\":\"Bob\",\"age\":30},\r\n    {\"name\":\"Charlie\",\"age\":35}\r\n]\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>การแปลง JSON Array กลับเป็น Array ของ Struct (Decoding)\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>สมมติว่าเรามีข้อมูล JSON ที่เป็น Array และต้องการแปลงกลับเป็น Array ของ struct ใน Go ให้ใช้ json.Unmarshal\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    jsonData := []byte(`[\r\n        {\"name\":\"Alice\",\"age\":25},\r\n        {\"name\":\"Bob\",\"age\":30},\r\n        {\"name\":\"Charlie\",\"age\":35}\r\n    ]`)\r\n\r\n    var people []Person\r\n    err := json.Unmarshal(jsonData, &amp;people)\r\n    if err != nil {\r\n        fmt.Println(\"Error decoding JSON:\", err)\r\n        return\r\n    }\r\n\r\n    for _, person := range people {\r\n        fmt.Println(person.Name, \"is\", person.Age, \"years old.\")\r\n    }\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>ผลลัพธ์:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">Alice is 25 years old.\r\nBob is 30 years old.\r\nCharlie is 35 years old.\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>สรุป\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fp>\u003Cul>\u003Cli>ใช้ json.Marshal เพื่อแปลง Array ของ struct เป็น JSON\u003C\u002Fli>\u003Cli>ใช้ json.Unmarshal เพื่อแปลง JSON Array กลับมาเป็น Array ของ struct\u003C\u002Fli>\u003C\u002Ful>","29_11zon_n0yyml94ow.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002F55v297j3bevlj7k\u002F29_11zon_n0yyml94ow.webp","2026-03-04 08:51:49.464Z","",{"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:27:15.893Z","keubtbdqa4mblx3","การพัฒนาโปรแกรม","2026-04-10 16:07:38.769Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:31:30.863Z","oyltq82epf0vqka","การเขียนโปรแกรม","2026-04-10 16:07:41.883Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:34:18.324Z","6xpqs57vsfjrf4z","Structs","2026-04-10 16:08:11.729Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:51:47.356Z","nbkqlahenx51dpr","JSON Array","2026-04-10 16:14:40.095Z",{"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,"expand":62,"id":76,"views":77},"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","d8h5wqgiutl8ne0",279,"55v297j3bevlj7k",[20,25,30,35,40,45],"2025-01-27 04:35:49.820Z","เรียนรู้วิธีการสร้างและจัดการ JSON Array ในภาษา Go เพื่อใช้ในการพัฒนาโปรแกรมที่มีประสิทธิภาพ","go-json-array","2026-04-22 07:11:50.212Z",{"th":82}]