How to read multiple JSON objects from one file in Go

huangapple go评论140阅读模式
英文:

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.MysqldbConfig.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 -->

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

<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

要完成这个任务,你需要将MysqlPostgres结构体包装到一个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, &amp;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), &amp;m)

try this:

package main

import (
	&quot;encoding/json&quot;
	&quot;fmt&quot;
)

func main() {
	m := make(map[string]Db)
	err := json.Unmarshal([]byte(input), &amp;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 = `{
  &quot;mysql&quot;: {
    &quot;address&quot;: &quot;127.0.0.1&quot;,
    &quot;port&quot;: &quot;3306&quot;,
    &quot;user&quot;: &quot;user&quot;,
    &quot;password&quot;: &quot;password&quot;,
    &quot;database&quot;: &quot;database&quot;
  },
  &quot;postgres&quot;: {
    &quot;address&quot;: &quot;127.0.0.2&quot;,
    &quot;port&quot;: &quot;3306&quot;,
    &quot;user&quot;: &quot;user2&quot;,
    &quot;password&quot;: &quot;password2&quot;,
    &quot;database&quot;: &quot;database2&quot;
  }
}`

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), &amp;m)

try this:

package main

import (
	&quot;encoding/json&quot;
	&quot;fmt&quot;
)

func main() {
	m := make(map[string]map[string]string)
	err := json.Unmarshal([]byte(input), &amp;m)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(m)
}

const input = `{
  &quot;mysql&quot;: {
    &quot;address&quot;: &quot;127.0.0.1&quot;,
    &quot;port&quot;: &quot;3306&quot;,
    &quot;user&quot;: &quot;user&quot;,
    &quot;password&quot;: &quot;password&quot;,
    &quot;database&quot;: &quot;database&quot;
  },
  &quot;postgres&quot;: {
    &quot;address&quot;: &quot;127.0.0.2&quot;,
    &quot;port&quot;: &quot;3306&quot;,
    &quot;user&quot;: &quot;user2&quot;,
    &quot;password&quot;: &quot;password2&quot;,
    &quot;database&quot;: &quot;database2&quot;
  }
}`

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]]

huangapple
  • 本文由 发表于 2017年8月29日 15:17:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/45932957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定