V
V
v- death2015-11-05 01:19:50
go
v- death, 2015-11-05 01:19:50

How to compose a type struct for this json?

Hello. There is json

{
  "MailConfig" : {
    "username": "*****@gmail.com",
    "password": "*****",
    "host":     "smtp.gmail.com",
    "port":     "587",
  },
  
  "MailTo" : [
    "*****@gmail.com",
    "*****@yandex.ru",
  ],

  "Types" : [
    "Debug",
    "Info",
    "Warn",
    "Error",
    "Fatal",
  ],

  "EmailSend" : {
    "Debug": false,
    "Info":  false,
    "Warn":  false,
    "Error": false,
    "Fatal": true,
  },

  "RedisConfig" : {
    "Addr":     "localhost:6379",
    "Password": "*****",
  },

  "MysqlConfig" : {
    "user" : "root",
    "pass" : "*****",
    "DB"   : "log"
  }
}

Tried to do something like this
package main

import (
  "encoding/json"
  "io/ioutil"
  "log"
)

func main() {
  bs, err := ioutil.ReadFile("log.json")
  if err != nil {
    log.Panicln(err)
  }

  b := []byte(bs)

  type Message struct {
    MailConfig, RedisConfig, MysqlConfig map[string]string
    MailTo                               []string
    Types                                []string
    EmailSend                            map[string]bool
  }

  var m Message

  err = json.Unmarshal(b, &m)
  if err != nil {
    log.Panicln(err)
  }
  log.Println(m)
}

terminal says
2015/11/05 00:18:33 invalid character '}' looking for beginning of object key string
panic: invalid character '}' looking for beginning of object key string


goroutine 1 [running]:
log.Panicln(0x1843ff40, 0x1, 0x1)
  /usr/lib/go/src/log/log.go:334 +0xa5
main.main()
  /home/v-smerti/localhost/api/src/microService/config/main.go:28 +0x1d1
exit status 2

Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey K, 2015-11-05
@mututunus

Your json is invalid.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question