Got error "invalid character 'ï' looking for beginning of value” from json.Unmarshal

后端 未结 1 521
礼貌的吻别
礼貌的吻别 2021-02-05 16:57

I use a Golang HTTP request to get json output as follow. The web service I am trying to access is Micrsoft Translator https://msdn.microsoft.com/en-us/library/dn876735.aspx

1条回答
  •  忘了有多久
    2021-02-05 17:54

    The server is sending you a UTF-8 text string with a Byte Order Mark (BOM). The BOM identifies that the text is UTF-8 encoded, but it should be removed before decoding.

    This can be done with the following line (using package "bytes"):

    body = bytes.TrimPrefix(body, []byte("\xef\xbb\xbf")) // Or []byte{239, 187, 191}
    

    PS. The error referring to ï is because the UTF-8 BOM interpreted as an ISO-8859-1 string will produce the characters .

    0 讨论(0)
提交回复
热议问题