简体   繁体   中英

In go, What is json.Unmarshall Maximum Depth?

I'm trying to parse JSON response that looks like this:

{
    "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:

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

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.

My question is, is there a maximum depth that json.Unmarshal can match? and if there is not, what am I doing wrong?

I don't think there is a maximum depth on 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. That should be the culprit for the missing fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM