为什么 Unmarshal 对嵌套结构失败?
Why Is Unmarshal Failing With A Nested Struct?
我正在尝试使用 Reddit 的 API 检索信息。 Here is some documentation on their json response, however, I got most of my information by just viewing the link in the browser and pretty-printing the response here.
以下代码在“回复”字段被注释掉时按预期运行,但在未注释掉时失败。
[edit] getData() 是我编写的一个函数,它使用 Go 的 http 客户端以字节为单位获取站点响应。
type redditThing struct {
Data struct {
Children []struct {
Data struct {
Permalink string
Subreddit string
Title string
Body string
Replies redditThing
}
}
}
}
func visitLink(link string) {
println("visiting:", link)
var comments []redditThing
if err := json.Unmarshal(getData(link+".json?raw_json=1"), &comments); err != nil {
logError.Println(err)
return
}
}
这将引发以下错误
json: cannot unmarshal string into Go struct field .Data.Children.Data.Replies.Data.Children.Data.Replies.Data.Children.Data.Replies of type main.redditThing
如有任何帮助,我们将不胜感激。提前谢谢大家!
[编辑] here link 某些数据导致程序失败
replies
字段可以是空字符串或 redditThing
。通过添加 Unmarshal 函数来处理空字符串来修复:
func (rt *redditThing) UnmarshalJSON(data []byte) error {
// Do nothing if data is the empty string.
if bytes.Equal(data, []byte(`""`)) {
return nil
}
// Prevent recursion by declaring type x with
// same underlying type as redditThing, but
// with no methods.
type x redditThing
return json.Unmarshal(data, (*x)(rt))
}
x
类型用于防止无限递归。如果方法的最后一行是 json.Unmarshal(data, rt)
,那么 json.Unmarshal
函数将调用 redditThing.UnmarshalJSON
方法,然后调用 json.Unmarshal
函数,依此类推。轰!
语句 type x redditThing
声明了一个名为 x
的新类型,其基础类型与 redditThing
相同。底层类型是匿名结构类型。底层类型没有方法,关键是,底层类型没有 UnmarshalJSON
方法。这可以防止递归。
我正在尝试使用 Reddit 的 API 检索信息。 Here is some documentation on their json response, however, I got most of my information by just viewing the link in the browser and pretty-printing the response here.
以下代码在“回复”字段被注释掉时按预期运行,但在未注释掉时失败。
[edit] getData() 是我编写的一个函数,它使用 Go 的 http 客户端以字节为单位获取站点响应。
type redditThing struct {
Data struct {
Children []struct {
Data struct {
Permalink string
Subreddit string
Title string
Body string
Replies redditThing
}
}
}
}
func visitLink(link string) {
println("visiting:", link)
var comments []redditThing
if err := json.Unmarshal(getData(link+".json?raw_json=1"), &comments); err != nil {
logError.Println(err)
return
}
}
这将引发以下错误
json: cannot unmarshal string into Go struct field .Data.Children.Data.Replies.Data.Children.Data.Replies.Data.Children.Data.Replies of type main.redditThing
如有任何帮助,我们将不胜感激。提前谢谢大家!
[编辑] here link 某些数据导致程序失败
replies
字段可以是空字符串或 redditThing
。通过添加 Unmarshal 函数来处理空字符串来修复:
func (rt *redditThing) UnmarshalJSON(data []byte) error {
// Do nothing if data is the empty string.
if bytes.Equal(data, []byte(`""`)) {
return nil
}
// Prevent recursion by declaring type x with
// same underlying type as redditThing, but
// with no methods.
type x redditThing
return json.Unmarshal(data, (*x)(rt))
}
x
类型用于防止无限递归。如果方法的最后一行是 json.Unmarshal(data, rt)
,那么 json.Unmarshal
函数将调用 redditThing.UnmarshalJSON
方法,然后调用 json.Unmarshal
函数,依此类推。轰!
语句 type x redditThing
声明了一个名为 x
的新类型,其基础类型与 redditThing
相同。底层类型是匿名结构类型。底层类型没有方法,关键是,底层类型没有 UnmarshalJSON
方法。这可以防止递归。