我们如何在golang中将json文件读取为json对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41135686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can we read a json file as json object in golang
提问by Aishwarya
I have a JSON file stored on the local machine. I need to read it in a variable and loop through it to fetch the JSON object values. If I use the Marshal command after reading the file using the ioutil.Readfile method, it gives some numbers as an output. These are my few failed attempts,
我在本地机器上存储了一个 JSON 文件。我需要在变量中读取它并循环遍历它以获取 JSON 对象值。如果我在使用 ioutil.Readfile 方法读取文件后使用 Marshal 命令,它会提供一些数字作为输出。这是我几次失败的尝试,
Attempt 1:
尝试 1:
plan, _ := ioutil.ReadFile(filename) // filename is the JSON file to read
var data interface{}
err := json.Unmarshal(plan, data)
if err != nil {
log.Error("Cannot unmarshal the json ", err)
}
fmt.Println(data)
It gave me following error,
它给了我以下错误,
time="2016-12-13T22:13:05-08:00" level=error msg="Cannot unmarshal the json json: Unmarshal(nil)"
<nil>
Attempt 2: I tried to store the JSON values in a struct and then using MarshalIndent
尝试 2:我尝试将 JSON 值存储在结构中,然后使用 MarshalIndent
generatePlan, _ := json.MarshalIndent(plan, "", " ") // plan is a pointer to a struct
fmt.Println(string(generatePlan))
It give me the output as string. But if I cast the output to string then I won't be able to loop it as JSON object.
它给我输出作为字符串。但是如果我将输出转换为字符串,那么我将无法将其作为 JSON 对象循环。
How can we read a JSON file as JSON object in golang? Is it possible to do that? Any help is appreciated. Thanks in advance!
我们如何在 golang 中将 JSON 文件读取为 JSON 对象?有可能这样做吗?任何帮助表示赞赏。提前致谢!
回答by John S Perayil
The value to be populated by json.Unmarshalneeds to be a pointer.
要填充的值json.Unmarshal需要是一个指针。
From GoDoc:
来自GoDoc:
Unmarshal parses the JSON-encoded data and stores the result in the value pointedto by v.
Unmarshal 解析 JSON 编码的数据并将结果存储在v指向的值中。
So you need to do the following :
因此,您需要执行以下操作:
plan, _ := ioutil.ReadFile(filename)
var data interface{}
err := json.Unmarshal(plan, &data)
Your error (Unmarshal(nil)) indicates that there was some problem in reading the file , please check the error returned by ioutil.ReadFile
您的错误 (Unmarshal(nil)) 表示读取文件时出现问题,请检查返回的错误 ioutil.ReadFile
Also please note that when using an empty interface in unmarshal, you would need to use type assertionto get the underlying values as go primitive types.
另请注意,在 unmarshal 中使用空接口时,您需要使用类型断言来获取底层值作为 go 原始类型。
To unmarshal JSON into an interface value, Unmarshal stores one of these in the interface value:
bool, for JSON booleans float64, for JSON numbers string, for JSON strings []interface{}, for JSON arrays map[string]interface{}, for JSON objects nil for JSON null
要将 JSON 解组为接口值,Unmarshal 将其中之一存储在接口值中:
bool, for JSON booleans float64, for JSON numbers string, for JSON strings []interface{}, for JSON arrays map[string]interface{}, for JSON objects nil for JSON null
It is always a much better approach to use a concrete structure to populate your json using Unmarshal.
使用具体结构来填充 json 始终是一种更好的方法Unmarshal。

