douhuan7862
2017-08-29 07:17
浏览 341
已采纳

如何在Go中从一个文件读取多个JSON对象

How can I read a JSON file where there is two different objects using Unmarshal ?

JSON example:

This is the structure corresponding to the JSON file.

{
  "mysql": {
    "address": "127.0.0.1",
    "port": "3306",
    "user": "user",
    "password": "password",
    "database": "database"
  },
  "postgres": {
    "address": "127.0.0.2",
    "port": "3306",
    "user": "user2",
    "password": "password2",
    "database": "database2"
  }
}

Code snippet:

type Database struct {
    Address  string
    Port     string
    User     string
    Password string
    Database string
}
type Mysql struct {
    Database
}
type Postgres struct {
    Database
}

图片转代码服务由CSDN问答提供 功能建议

如何使用Unmarshal读取存在两个不同对象的JSON文件?

JSON示例:

这是与JSON文件相对应的结构。

  {
“ mysql”:{\  n“地址”:“ 127.0.0.1”,
“端口”:“ 3306”,
“用户”:“用户”,
“密码”:“密码”,
“数据库”:“数据库”  
},
“ postgres”:{
“ address”:“ 127.0.0.2”,
“ port”:“ 3306”,
“ user”:“ user2”,
“ password”:“  password2“,
”数据库“:”数据库2“ 
} 
} 
   
 
 

代码段:
  type数据库结构{
地址字符串
端口字符串
用户字符串
密码字符串
数据库字符串
} 
type Mysql结构{
数据库
}  
type Postgres结构{
数据库
} 
   
 

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • drlndkhib08556095 2017-08-29 07:17
    已采纳

    To do this, you need to wrap the Mysql and Postgres structure into a Configuration structure then pass it to Unmarshal function:

    type Configuration struct {
        Mysql    Mysql
        Postgres Postgres
    }
    
    func main() {
        content, err := ioutil.ReadFile(confPath)
        var conf Configuration
        err = json.Unmarshal(content, &conf)
    }
    

    See full working example: https://play.golang.org./p/7CtALgsjK3

    Hope this will help some people.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dsjojts9734 2017-08-29 09:32

    Just another way:

    m := make(map[string]Db)
    err := json.Unmarshal([]byte(input), &m)
    

    try this:

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    func main() {
        m := make(map[string]Db)
        err := json.Unmarshal([]byte(input), &m)
        if err != nil {
            fmt.Println(err)
        }
        fmt.Println(m)
    }
    
    type Db struct {
        Address  string
        Port     string
        User     string
        Password string
        Database string
    }
    
    const input = `{
      "mysql": {
        "address": "127.0.0.1",
        "port": "3306",
        "user": "user",
        "password": "password",
        "database": "database"
      },
      "postgres": {
        "address": "127.0.0.2",
        "port": "3306",
        "user": "user2",
        "password": "password2",
        "database": "database2"
      }
    }`
    

    output:

    map[mysql:{127.0.0.1 3306 user password database} postgres:{127.0.0.2 3306 user2 password2 database2}]
    

    Even this works for you:

    m := make(map[string]map[string]string)
    err := json.Unmarshal([]byte(input), &m)
    

    try this:

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    func main() {
        m := make(map[string]map[string]string)
        err := json.Unmarshal([]byte(input), &m)
        if err != nil {
            fmt.Println(err)
        }
        fmt.Println(m)
    }
    
    const input = `{
      "mysql": {
        "address": "127.0.0.1",
        "port": "3306",
        "user": "user",
        "password": "password",
        "database": "database"
      },
      "postgres": {
        "address": "127.0.0.2",
        "port": "3306",
        "user": "user2",
        "password": "password2",
        "database": "database2"
      }
    }`
    

    output:

    map[mysql:map[address:127.0.0.1 port:3306 user:user password:password database:database] postgres:map[database:database2 address:127.0.0.2 port:3306 user:user2 password:password2]]
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题