我正在try 用Golang解析一个json数组,格式如下:
[
{
"table1": [
{ "name": "col1", "type": "string"},
{ "name": "col2", "type": "int"},
]
},
{
"table2": [
{ "name": "col3", "type": "string"},
{ "name": "col4", "type": "int"},
]
},
{
"table3": [
...
]
}
]
例如,json应该包含不同表的模式. 我try 了以下代码,但架构返回空:
package main
import (
"encoding/json"
"io"
"log"
"os"
)
type ColumnType struct {
Name string `json:"name"`
Type string `json:"type"`
}
type Schema struct {
Schema map[string][]ColumnType
}
func main() {
mocksSchemas, _ := os.Open("parse_config/mock_schema.json")
var schemas []Schema
content, err := io.ReadAll(mocksSchemas)
if err != nil {
log.Fatal("Error when reading mock file: ", err)
}
err = json.Unmarshal(content, &schemas)
if err != nil {
log.Fatal("Error during Unmarshal(): ", err)
}
defer mocksSchemas.Close()
}
我做错了什么?如果能帮我指出我的错误,我将不胜感激 谢谢!