LoginSignup
28
28

More than 1 year has passed since last update.

JSON-TO-GO を使えば、GO 構造体定義がすこぶる簡単!

Posted at

Teratail の質問 API

プログラミングに関する質問ができる Teratail では、API を提供しています。

質問一覧では、下記のようなレスポンスが返ってくるとのことです。

これを一つ一つ Go の構造体を作成するのは結構時間がかかる。。

{
  "meta": {
    "message": "success",
    "total_page": 1,
    "page": 1,
    "limit": 10,
    "hit_num": 2
  },
  "questions": [
    {
      "id": 10364,
      "title": "インデントにタブを使うことのデメリット",
      "created": "2015-05-27 04:49:55",
      "modified": "2015-06-22 15:17:06",
      "count_reply": 11,
      "count_clip": 11,
      "count_pv": 0,
      "is_beginner": false,
      "is_accepted": false,
      "is_presentation": false,
      "tags": [
        "Eclipse",
        "タブ",
        "プログラミング言語",
        "コーディング規約"
      ],
      "user": {
        "display_name": "miu_ras",
        "photo": "https://cdn.teratail.com/uploads/avatars/15047/4DVbsgOW_thumbnail.jpg",
        "score": 101
      }
    },
    {
      "id": 9399,
      "title": "mysqlに格納された配列データで絞り込みをかけることはできるのでしょうか?",
      "created": "2015-05-04 15:59:29",
      "modified": "2015-05-04 15:59:29",
      "count_reply": 2,
      "count_clip": 0,
      "count_pv": 0,
      "is_beginner": true,
      "is_accepted": true,
      "is_presentation": false,
      "tags": [
        "MySQL"
      ],
      "user": {
        "display_name": "mendosa",
        "photo": "https://tt4.leverages.org/uploads/avatars/13696/4WwsssAn_thumbnail.jpg",
        "score": 7
      }
    }
  ]
}

そこで使うのが、JSON-To-GO

さっそく JSON-TO-GO に貼り付ける

単純にコピペするだけで、構造体を作ってくる

Screen Shot 2021-07-31 at 22.05.41.png

インラインでネストした場合

type AutoGenerated struct {
    Meta struct {
        Message   string `json:"message"`
        TotalPage int    `json:"total_page"`
        Page      int    `json:"page"`
        Limit     int    `json:"limit"`
        HitNum    int    `json:"hit_num"`
    } `json:"meta"`
    Questions []struct {
        ID             int      `json:"id"`
        Title          string   `json:"title"`
        Created        string   `json:"created"`
        Modified       string   `json:"modified"`
        CountReply     int      `json:"count_reply"`
        CountClip      int      `json:"count_clip"`
        CountPv        int      `json:"count_pv"`
        IsBeginner     bool     `json:"is_beginner"`
        IsAccepted     bool     `json:"is_accepted"`
        IsPresentation bool     `json:"is_presentation"`
        Tags           []string `json:"tags"`
        User           struct {
            DisplayName string `json:"display_name"`
            Photo       string `json:"photo"`
            Score       int    `json:"score"`
        } `json:"user"`
    } `json:"questions"`
}

ネストしない場合

type AutoGenerated struct {
    Meta      Meta        `json:"meta"`
    Questions []Questions `json:"questions"`
}
type Meta struct {
    Message   string `json:"message"`
    TotalPage int    `json:"total_page"`
    Page      int    `json:"page"`
    Limit     int    `json:"limit"`
    HitNum    int    `json:"hit_num"`
}
type User struct {
    DisplayName string `json:"display_name"`
    Photo       string `json:"photo"`
    Score       int    `json:"score"`
}
type Questions struct {
    ID             int      `json:"id"`
    Title          string   `json:"title"`
    Created        string   `json:"created"`
    Modified       string   `json:"modified"`
    CountReply     int      `json:"count_reply"`
    CountClip      int      `json:"count_clip"`
    CountPv        int      `json:"count_pv"`
    IsBeginner     bool     `json:"is_beginner"`
    IsAccepted     bool     `json:"is_accepted"`
    IsPresentation bool     `json:"is_presentation"`
    Tags           []string `json:"tags"`
    User           User     `json:"user"`
}
28
28
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28
28