簡體   English   中英

在進行中,什么是json.Unmarshall最大深度?

[英]In go, What is json.Unmarshall Maximum Depth?

我正在嘗試解析如下所示的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!"
                    }
                }
            ]
        }
    ]
}

我正在使用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
            }
        }
    }
}

但是, json.UnmarshalMessaging任何結構的JSON響應都不匹配

此函數完全重現了該問題:

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)
}

這是輸出:

{Object:page Entry:[{Id:185985174761277 Time:1462333588680 Messaging:[{Sender:{Id:} Recipient:{Id:} Timestamp:0 Message:{Mid: Seq: Text:}}]}]}

如您所見,Message結構內的所有字段均未設置。

我的問題是,json.Unmarshal可以匹配的最大深度嗎? 如果沒有,我在做什么錯?

我認為json.Unmarshal上沒有最大深度。

在您的代碼中, Message字段中的Seq字段定義為字符串, SenderRecipient中的Id字段也定義為字符串,而在json中,它們是整數。 那應該是缺少字段的罪魁禍首。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM