假设我有一个如下所示的结构:-
//easyjson:json
type JSONData struct {
Data []string
}
我想将下面的 json 解码为 JSONData
结构
{"Data" : ["One", "Two", "Three"]}
谁能告诉我如何使用 easyjson在 Golang 中取消编码一个 json?我在他们的 README
最佳答案
我不知道你为什么要尝试使用 easyjson。 encoding/json 非常适合使用。但尽管这是给你的答案。
注意:如果使用encoding/json会更好。
//easyjson:json
type JSONData struct {
Data []string
}
定义此结构后运行 easyjson <fileName-JSONData-is-defined>.go
.这将创建一个额外的 go 文件包含
func (v JSONData) MarshalJSON() ([]byte, error)
func (v JSONData) MarshalEasyJSON(w *jwriter.Writer)
func (v *JSONData) UnmarshalJSON(data []byte) errorfunc (v *JSONData)
func UnmarshalEasyJSON(l *jlexer.Lexer)
那些方法。 然后(取消)编码使用
d := &JSONData{}
d.UnmarshalJSON([]byte(`{"Data" : ["One", "Two", "Three"]} `))
// Or you could also use
// json.Unmarshal(data, d) this will also call this d.UnmarshalJSON
fmt.Println(d)
一个完整的例子是 here.
关于json - 将 EasyJSON 与 golang 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40587860/