質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Go

Go(golang)は、Googleで開発されたオープンソースのプログラミング言語です。

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

JSON

JSON(JavaScript Object Notation)は軽量なデータ記述言語の1つである。構文はJavaScriptをベースとしていますが、JavaScriptに限定されたものではなく、様々なソフトウェアやプログラミング言語間におけるデータの受け渡しが行えるように設計されています。

Q&A

解決済

1回答

374閲覧

golangでAPIからJSONの構造体の値があるときとないときの場合のエラー処理

seven77

総合スコア13

Go

Go(golang)は、Googleで開発されたオープンソースのプログラミング言語です。

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

JSON

JSON(JavaScript Object Notation)は軽量なデータ記述言語の1つである。構文はJavaScriptをベースとしていますが、JavaScriptに限定されたものではなく、様々なソフトウェアやプログラミング言語間におけるデータの受け渡しが行えるように設計されています。

0グッド

0クリップ

投稿2018/04/25 05:26

Google Book APIから以下のようにレスポンス(①)が来るので以下のように構造体(②)を作りました。

JSON

1{ 2 "kind": "books#volumes", 3 "totalItems": 1, 4 "items": [ 5 { 6 "kind": "books#volume", 7 "id": "xMMeNwAACAAJ", 8 "etag": "M8bNz6/uLH4", 9 "selfLink": "https://www.googleapis.com/books/v1/volumes/xMMeNwAACAAJ", 10 "volumeInfo": { 11 "title": "カラフル", 12 "authors": [ 13 "森絵都" 14 ], 15 "publishedDate": "2007-09", 16 "description": "生前の罪により、輪廻のサイクルから外されたぼくの魂。だが天使業界の抽選にあたり、再挑戦のチャンスを得た。自殺を図った少年、真の体にホームステイし、自分の罪を思い出さなければならないのだ。真として過ごすうち、ぼくは人の欠点や美点が見えてくるようになるのだが...。不朽の名作ついに登場。", 17 "industryIdentifiers": [ 18 { 19 "type": "ISBN_10", 20 "identifier": "4167741016" 21 }, 22 { 23 "type": "ISBN_13", 24 "identifier": "9784167741013" 25 } 26 ], 27 "readingModes": { 28 "text": false, 29 "image": false 30 }, 31 "pageCount": 259, 32 "printType": "BOOK", 33 "categories": [ 34 "Fantasy fiction" 35 ], 36 "averageRating": 3.5, 37 "ratingsCount": 27, 38 "maturityRating": "NOT_MATURE", 39 "allowAnonLogging": false, 40 "contentVersion": "preview-1.0.0", 41 "imageLinks": { 42 "smallThumbnail": "http://books.google.com/books/content?id=xMMeNwAACAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api", 43 "thumbnail": "http://books.google.com/books/content?id=xMMeNwAACAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api" 44 }, 45 "language": "ja", 46 "previewLink": "http://books.google.co.jp/books?id=xMMeNwAACAAJ&dq=isbn:9784167741013&hl=&cd=1&source=gbs_api", 47 "infoLink": "http://books.google.co.jp/books?id=xMMeNwAACAAJ&dq=isbn:9784167741013&hl=&source=gbs_api", 48 "canonicalVolumeLink": "https://books.google.com/books/about/%E3%82%AB%E3%83%A9%E3%83%95%E3%83%AB.html?hl=&id=xMMeNwAACAAJ" 49 }, 50 "saleInfo": { 51 "country": "JP", 52 "saleability": "NOT_FOR_SALE", 53 "isEbook": false 54 }, 55 "accessInfo": { 56 "country": "JP", 57 "viewability": "NO_PAGES", 58 "embeddable": false, 59 "publicDomain": false, 60 "textToSpeechPermission": "ALLOWED", 61 "epub": { 62 "isAvailable": false 63 }, 64 "pdf": { 65 "isAvailable": false 66 }, 67 "webReaderLink": "http://play.google.com/books/reader?id=xMMeNwAACAAJ&hl=&printsec=frontcover&source=gbs_api", 68 "accessViewStatus": "NONE", 69 "quoteSharingAllowed": false 70 }, 71 "searchInfo": { 72 "textSnippet": "生前の罪により、輪廻のサイクルから外されたぼくの魂。だが天使業界の抽選にあたり、再挑戦のチャンスを得た。自殺を図った少年、真の体にホームステイし、自分の罪を思い ..." 73 } 74 } 75 ] 76}

JSON

1type GoogleBook struct { 2 Kind string `json:"kind"` 3 TotalItems int `json:"totalItems"` 4 Items []struct { 5 Kind string `json:"kind"` 6 ID string `json:"id"` 7 Etag string `json:"etag"` 8 SelfLink string `json:"selfLink"` 9 VolumeInfo struct { 10 Title string `json:"title"` 11 Authors []string `json:"authors"` 12 PublishedDate string `json:"publishedDate"` 13 Description string `json:"description"` 14 IndustryIdentifiers []struct { 15 Type string `json:"type"` 16 Identifier string `json:"identifier"` 17 } `json:"industryIdentifiers"` 18 ReadingModes struct { 19 Text bool `json:"text"` 20 Image bool `json:"image"` 21 } `json:"readingModes"` 22 PageCount int `json:"pageCount"` 23 PrintType string `json:"printType"` 24 Categories []string `json:"categories"` 25 AverageRating float64 `json:"averageRating"` 26 RatingsCount int `json:"ratingsCount"` 27 MaturityRating string `json:"maturityRating"` 28 AllowAnonLogging bool `json:"allowAnonLogging"` 29 ContentVersion string `json:"contentVersion"` 30 ImageLinks struct { 31 SmallThumbnail string `json:"smallThumbnail"` 32 Thumbnail string `json:"thumbnail"` 33 } `json:"imageLinks"` 34 Language string `json:"language"` 35 PreviewLink string `json:"previewLink"` 36 InfoLink string `json:"infoLink"` 37 CanonicalVolumeLink string `json:"canonicalVolumeLink"` 38 } `json:"volumeInfo"` 39 SaleInfo struct { 40 Country string `json:"country"` 41 Saleability string `json:"saleability"` 42 IsEbook bool `json:"isEbook"` 43 } `json:"saleInfo"` 44 AccessInfo struct { 45 Country string `json:"country"` 46 Viewability string `json:"viewability"` 47 Embeddable bool `json:"embeddable"` 48 PublicDomain bool `json:"publicDomain"` 49 TextToSpeechPermission string `json:"textToSpeechPermission"` 50 Epub struct { 51 IsAvailable bool `json:"isAvailable"` 52 } `json:"epub"` 53 Pdf struct { 54 IsAvailable bool `json:"isAvailable"` 55 } `json:"pdf"` 56 WebReaderLink string `json:"webReaderLink"` 57 AccessViewStatus string `json:"accessViewStatus"` 58 QuoteSharingAllowed bool `json:"quoteSharingAllowed"` 59 } `json:"accessInfo"` 60 SearchInfo struct { 61 TextSnippet string `json:"textSnippet"` 62 } `json:"searchInfo"` 63 } `json:"items"` 64} 65

ISBNがはいっている③がある時と無い時があり、それのエラー処理の仕方がわかりません。

JSON

1IndustryIdentifiers []struct { 2 Type string `json:"type"` 3 Identifier string `json:"identifier"` 4 } `json:"industryIdentifiers"` 5 67 8"industryIdentifiers": [ 9 { 10 "type": "ISBN_10", 11 "identifier": "4167741016" 12 }, 13 { 14 "type": "ISBN_13", 15 "identifier": "9784167741013" 16 } 17 ],

③がない場合は、ISBN_10, ISBN_13は空であるようにしたいです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

こういったAPI応答の解釈は2段階あって、

  • JSONとしての解釈
  • アプリケーションとしての解釈

③があるかないかにかかわらずJSONとしての解釈は可能なはずです。
あらためてIndustryIdentifiersの長さをチェックしてください。
それをエラーとするかどうかはアプリの要件次第です。

投稿2018/04/26 05:55

nobonobo

総合スコア3367

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問