[英]In go, What is json.Unmarshall Maximum Depth?
I'm trying to parse JSON response that looks like this: 我正在尝试解析如下所示的JSON响应:
{
"object": "page",
"entry": [
{
"id": 185985174761277,
"time": 1462333588680,
"messaging": [
{
"sender": {
"id": 1053704801343033
},
"recipient": {
"id": 185985174761277
},
"timestamp": 1462333588645,
"message": {
"mid": "mid.1462333588639:d44f4374dfc510c351",
"seq": 1948,
"text": "Hello World!"
}
}
]
}
]
}
I'm using json.Unmarshal
and passing the following struct as the interface: 我正在使用
json.Unmarshal
并将以下结构作为接口传递:
type Message struct {
Object string
Entry []struct {
Id int64
Time int64
Messaging []struct {
Sender struct {
Id string
}
Recipient struct {
Id string
}
Timestamp int64
Message struct {
Mid string
Seq string
Text string
}
}
}
}
However, json.Unmarshal
does not match the JSON Response to any of the structs in Messaging
但是,
json.Unmarshal
与Messaging
任何结构的JSON响应都不匹配
This function reproduces the issue exactly: 此函数完全重现了该问题:
type Message struct {
Object string
Entry []struct {
Id int64
Time int64
Messaging []struct {
Sender struct {
Id string
}
Recipient struct {
Id string
}
Timestamp int64
Message struct {
Mid string
Seq string
Text string
}
}
}
}
func testStruct() {
jsonResponse := []byte(`{
"object": "page",
"entry": [
{
"id": 185985174761277,
"time": 1462333588680,
"messaging": [
{
"sender": {
"id": 1053704801343033
},
"recipient": {
"id": 185985174761277
},
"timestamp": 1462333588645,
"message": {
"mid": "mid.1462333588639:d44f4374dfc510c351",
"seq": 1948,
"text": "oijsdfoijsdfoij"
}
}
]
}
]
}`)
var m Message
json.Unmarshal(jsonResponse, &m)
fmt.Println(string(jsonResponse))
fmt.Printf("%+v\n", m)
}
This is the output: 这是输出:
{Object:page Entry:[{Id:185985174761277 Time:1462333588680 Messaging:[{Sender:{Id:} Recipient:{Id:} Timestamp:0 Message:{Mid: Seq: Text:}}]}]}
and as you can see, all the fields inside the Message struct are not set. 如您所见,Message结构内的所有字段均未设置。
My question is, is there a maximum depth that json.Unmarshal can match? 我的问题是,json.Unmarshal可以匹配的最大深度吗? and if there is not, what am I doing wrong?
如果没有,我在做什么错?
I don't think there is a maximum depth on json.Unmarshal. 我认为json.Unmarshal上没有最大深度。
In your code, Seq
field in Message
field is defined as string, so are Id
fields in Sender
and Recipient
, while in json they are integers. 在您的代码中,
Message
字段中的Seq
字段定义为字符串, Sender
和Recipient
中的Id
字段也定义为字符串,而在json中,它们是整数。 That should be the culprit for the missing fields. 那应该是缺少字段的罪魁祸首。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.