Непревзойденный JSON на Голанге - PullRequest
0 голосов
/ 05 января 2019

новичок Голанга здесь.

Я хочу отменить вывод JSON, показанного здесь:

 {
  "intro": {
    "title": "The Little Blue Gopher",
    "story": [
      "Once upon a time, long long ago, there was a little blue gopher. Our little blue friend wanted to go on an adventure, but he wasn't sure where to go. Will you go on an adventure with him?",
      "One of his friends once recommended going to New York to make friends at this mysterious thing called \"GothamGo\". It is supposed to be a big event with free swag and if there is one thing gophers love it is free trinkets. Unfortunately, the gopher once heard a campfire story about some bad fellas named the Sticky Bandits who also live in New York. In the stories these guys would rob toy stores and terrorize young boys, and it sounded pretty scary.",
      "On the other hand, he has always heard great things about Denver. Great ski slopes, a bad hockey team with cheap tickets, and he even heard they have a conference exclusively for gophers like himself. Maybe Denver would be a safer place to visit."
    ],
    "options": [
      {
        "text": "That story about the Sticky Bandits isn't real, it is from Home Alone 2! Let's head to New York.",
        "arc": "new-york"
      },
      {
        "text": "Gee, those bandits sound pretty real to me. Let's play it safe and try our luck in Denver.",
        "arc": "denver"
      }
    ]
  },...}

В карту [строка] Контекст.

Вот соответствующие определения:

type Context struct {
    title   string
    story   string 
    options *[]Option
}

type Option struct {
    text string
    arc  string
}

Unmarshall работает без ошибок, однако Я получаю карту [intro] со структурой Context, в которой все инициализировано как nils или пустые строки.

Как правильно это сделать? Документацию и примеры просто очень сложно проанализировать для конкретных случаев использования.

редактирование: Есть еще один вопрос, который может быть дубликатом, но этот вопрос немного отличается, поскольку для правильной работы требуются строковые теги.

1 Ответ

0 голосов
/ 05 января 2019

Для сортировки и отмены сортировки поля должны быть экспортированы.

    type Context struct {
        Title   string   `json:"title"`
        Story   string   `json:"story"`
        Options []Option `json:"options"`
    }

    type Option struct {
        Text string `json:"text`
        Arc  string `json:"arc"`
    }
...