英文:
How to read multiple JSON objects from one file in Go
问题
如何使用Unmarshal读取包含两个不同对象的JSON文件?
以下是相应的JSON文件结构示例:
{
"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"
}
}
以下是代码片段示例:
type Database struct {
Address string
Port string
User string
Password string
Database string
}
type Mysql struct {
Database
}
type Postgres struct {
Database
}
你可以使用json.Unmarshal
函数将JSON文件解析为相应的结构体对象。例如:
import (
"encoding/json"
"fmt"
"io/ioutil"
)
func main() {
// 读取JSON文件内容
data, err := ioutil.ReadFile("path/to/your/json/file.json")
if err != nil {
fmt.Println("读取文件错误:", err)
return
}
// 解析JSON文件
var dbConfig struct {
Mysql Mysql
Postgres Postgres
}
err = json.Unmarshal(data, &dbConfig)
if err != nil {
fmt.Println("解析JSON错误:", err)
return
}
// 访问解析后的对象
fmt.Println("MySQL配置:")
fmt.Println("地址:", dbConfig.Mysql.Address)
fmt.Println("端口:", dbConfig.Mysql.Port)
fmt.Println("用户:", dbConfig.Mysql.User)
fmt.Println("密码:", dbConfig.Mysql.Password)
fmt.Println("数据库:", dbConfig.Mysql.Database)
fmt.Println("Postgres配置:")
fmt.Println("地址:", dbConfig.Postgres.Address)
fmt.Println("端口:", dbConfig.Postgres.Port)
fmt.Println("用户:", dbConfig.Postgres.User)
fmt.Println("密码:", dbConfig.Postgres.Password)
fmt.Println("数据库:", dbConfig.Postgres.Database)
}
请将代码中的"path/to/your/json/file.json"
替换为你实际的JSON文件路径。解析后,你可以通过dbConfig.Mysql
和dbConfig.Postgres
访问相应的对象属性。
英文:
How can I read a JSON file where there is two different objects using Unmarshal ?
<h2>JSON example:</h2>
This is the structure corresponding to the JSON file.
<!-- language: json -->
{
"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"
}
}
<h2>Code snippet:</h2>
<!-- language: lang-go -->
type Database struct {
Address string
Port string
User string
Password string
Database string
}
type Mysql struct {
Database
}
type Postgres struct {
Database
}
答案1
得分: 5
要完成这个任务,你需要将Mysql
和Postgres
结构体包装到一个Configuration
结构体中,然后将其传递给Unmarshal
函数:
type Configuration struct {
Mysql Mysql
Postgres Postgres
}
func main() {
content, err := ioutil.ReadFile(confPath)
var conf Configuration
err = json.Unmarshal(content, &conf)
}
完整的工作示例请参考:https://play.golang.org./p/7CtALgsjK3
希望这对一些人有所帮助。
英文:
To do this, you need to wrap the Mysql
and Postgres
structure into a Configuration
structure then pass it to Unmarshal function:
<!-- language: lang-go -->
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.
答案2
得分: 1
只是另一种方式:
m := make(map[string]Db)
err := json.Unmarshal([]byte(input), &m)
尝试这个:
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"
}
}`
输出:
map[mysql:{127.0.0.1 3306 user password database} postgres:{127.0.0.2 3306 user2 password2 database2}]
甚至这个也适用于你:
m := make(map[string]map[string]string)
err := json.Unmarshal([]byte(input), &m)
尝试这个:
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"
}
}`
输出:
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]]
英文:
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]]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论