実現したいこと
goでjsonファイルを読み込んでDBに挿入したいです。
gormを用いてマイグレーションでテーブルを作成して作成したテーブルにjsonファイルの情報を格納したいです。
jsonのスライスをそのままDBに保存はできないと思ったので
jsonファイルを読み込んだ後、Movies.CastとMovies.Genresのスライス情報はstrings.Joinでstringに変換してDBに挿入すればよいのかなと考えています。
DBにmigrationを用いてDBにテーブルは作成済みです。
前提
Go初学者です。gin,gormを用いて映画検索のwebページを作成しています。
発生している問題・エラーメッセージ
unsupported data type: &[]
該当のソースコード
main.go
1func main () { 2 // jsonファイルを読み込む 3 jsonData, _ := os.ReadFile("./movies.json") 4 5 // 構造体のスライスを作成 6 var movies []models.Movie 7 8 err := json.Unmarshal(jsonData, &movies) 9 if err != nil { 10 fmt.Println("error:", err) 11 } 12 13 for _, movie := range movies { 14 strings.Join(movie.Cast, "/") 15 fmt.Println(movie.Cast) 16 initializers.DB.Create(&movie) 17 18 } 19}
movies.json
1[ 2 { 3 "title": "Winter Day Dreams ft. Franny's Feet and Olivia", 4 "year": 2010, 5 "cast": [ 6 "Franny's Feet", 7 "Phoebe McAuley", 8 "George Buza", 9 "Katherine Crimi", 10 "Emily Gray" 11 ], 12 "genres": [ 13 "Animated", 14 "Family" 15 ], 16 "href": "Franny%27s_Feet", 17 "extract": "Franny's Feet is a Canadian animated children's television series created by writer Cathy Moss and fellow Susin Nielsen. The series was produced by Decode Entertainment, in association with Channel Five Broadcasting Limited. The series follows the adventures of Frances \"Franny\" Fantootsie as she tries on a new shoe and travels to different countries.", 18 "thumbnail": "https://upload.wikimedia.org/wikipedia/commons/2/23/Frannys_Feet_Logo.png", 19 "thumbnail_width": 200, 20 "thumbnail_height": 108 21 }, 22 { 23 "title": "Daybreakers", 24 "year": 2010, 25 "cast": [ 26 "Ethan Hawke", 27 "Willem Dafoe", 28 "Sam Neill", 29 "Claudia Karvan", 30 "Michael Dorman", 31 "Isabel Lucas", 32 "Vince Colosimo", 33 "Jay Laga'aia" 34 ], 35 "genres": [ 36 "Action", 37 "Horror", 38 "Science Fiction" 39 ], 40 "href": "Daybreakers", 41 "extract": "Daybreakers is a 2009 science-fiction action horror film written and directed by Michael and Peter Spierig. The film takes place in a futuristic world overrun by vampires, and centers around a vampiric corporation which sets out to capture and farm the remaining humans while researching a substitute for human blood. Ethan Hawke plays vampire hematologist Edward Dalton, whose work is interrupted by human survivors led by former vampire \"Elvis\", who has a cure that can save the human species.", 42 "thumbnail": "https://upload.wikimedia.org/wikipedia/en/b/b4/Daybreakers_ver2.jpg", 43 "thumbnail_width": 259, 44 "thumbnail_height": 383 45 } 46] 47
movies.go
1type Movie struct { 2 gorm.Model 3 Title string 4 Year int16 5 Cast []string //スライスで定義したいができなかった 6 Genres []string //スライスで定義したいができなかった 7 Href string 8 Extract string 9 Thumbnail string 10 ThumbnailWidth int16 11 ThumbnailHeight int16 12}
試したこと
実行するとmain.go 16行目で
unsupported data type: &[]が発生
gormではスライス型は受け付けていないので、model.MovieのCastとGenresを普通のstringに変更してと思ったのですが
[]string→string
これもjsonの中身をModel.Movieの方にあてはめられないので方法が見つかりませんでした。
jsonのスライスをそのままDBに入れる方法や私が行ったsliceからstringに変換してDBに挿入する方法で修正箇所があれば教えていただきたいです。よろしくお願いいたします。
補足情報(FW/ツールのバージョンなど)
go v 1.20
windows

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。