我有一个样本json文件,其结构如下
{
"method":"brute_force",
"bc":"select * from blah;",
"gc":[
"select sum(year) from blah;",
"select count(*) from table;"
]
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试编写一个可以读取此文件并运行json内容的go程序.
package main
import (
"fmt"
"encoding/json"
"io/ioutil"
)
type Response2 struct {
method string
bc string
gc []string
}
func main() {
file,_ := ioutil.ReadFile("config.json")
fmt.Printf("%s",string(file))
res := &Response2{}
json.Unmarshal([]byte(string(file)), &res)
fmt.Println(res)
fmt.Println(res.method)
fmt.Println(res.gc)
}
Run Code Online (Sandbox Code Playgroud)
res.method和res.gc不打印任何东西.我不知道什么是错的.
type Response2 struct {
method string
bc string
gc []string
}
Run Code Online (Sandbox Code Playgroud)
字段的名称必须为大写,否则Json模块无法访问它们(它们对您的模块是私有的).您可以使用json标记指定Field和name之间的匹配项
type Response2 struct {
Method string `json:"method"`
Bc string `json:"bc"`
Gc []string `json:"gc"`
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3911 次 |
最近记录: |