2016-05-04 45 views
0

我試圖解析看起來像這樣JSON響應:在去,什麼是json.Unmarshall最大深度?

{ 
    "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.Unmarshal不匹配JSON對Messaging中的任何結構的響應

此函數完全重現問題:

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:}}]}]} 

,正如你所看到的,所有的信息結構中的字段沒有設置。

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

回答

3

我不認爲json.Unmarshal有最大深度。

在你的代碼,在MessageSeq字段定義爲字符串,所以在SenderRecipientId領域,而在JSON他們都是整數。這應該是缺失領域的罪魁禍首。

+0

Int64,因爲它們很大:) – smnpl

+0

並且請不要忽略返回的錯誤。 –

+0

這是確切的問題。我沒有意識到在這些字段中我有字符串而不是int64!謝謝。 – ILikeTacos