无效字符’d’,寻找值的开头。

huangapple go评论127阅读模式
英文:

invalid character 'd' looking for beginning of value

问题

我使用Python将一些数据发送到服务器,并在发送数据时使用了json.dumps()。

  1. data = {
  2. 'reason': 'invalid',
  3. 'dtype': 120,
  4. 'id': 708,
  5. 'scene': 'external_vender'
  6. }
  7. send the data as json.dumps(data)

但是在使用Go读取服务器端的数据时,我遇到了以下错误。有人知道可能是什么原因导致了这个问题吗?

  1. unmarshal err=invalid character 'd' looking for beginning of value
  2. type Response struct {
  3. id int64 `json:"id"`
  4. dtype int32 `json:"type"`
  5. reason string `json:"reason"`
  6. scene string `json:"scene"`
  7. }
  8. var response Response
  9. err := json.Unmarshal(Data, &response)
英文:

I send some data to the server using python and use json.dumps() while sending the data.

  1. data = {
  2. 'reason': 'invalid',
  3. 'dtype': 120,
  4. 'id': 708,
  5. 'scene': 'external_vender'
  6. }
  7. send the data as json.dumps(data)

but while reading the data at the server-side using Go, I am getting following error. Does anyone know what meybe the possible cause of this issue?

unmarshal err=invalid character 'd' looking for beginning of value

  1. type Response struct {
  2.     id int64   `json:"id"`
  3.     dtype int32  `json:"type"`
  4. reason string  `json:"reason"`
  5.     scene string  `json:"scene"`
  6. }
  7. var response Response
  8. err := json.Unmarshal(Data, &response)

答案1

得分: 1

json.Unmarshall 只能用于首字母大写的字段。

将这段代码修改为:

  1. type Response struct {
  2. Id int64 `json:"id"`
  3. Dtype int32 `json:"dtype"`
  4. Reason string `json:"reason"`
  5. Scene string `json:"scene"`
  6. }

这可能不是你唯一的错误,因为 invalid character 'd' looking for beginning of value 暗示了其他的解析错误。尝试在调用 Unmarhsall 之前打印出 Data,看看它是否是有效的 JSON。

英文:

json.Unmarshall only works on fields with capital letters.

Change this:

  1. type Response struct {
  2. id int64 `json:”id”`
  3. dtype int32 `json:”dtype”`
  4. reason string `json:”reason”`
  5. scene string `json:”scene”`
  6. }

To this:

  1. type Response struct {
  2. Id int64 `json:"id"`
  3. Dtype int32 `json:"dtype"`
  4. Reason string `json:"reason"`
  5. Scene string `json:"scene"`
  6. }

That may not be your only error - as the invalid character 'd' looking for beginning of value implies some other parsing error. Trying printing out Data before calling Unmarhsall to see if it looks like valid json.

huangapple
  • 本文由 发表于 2022年4月12日 15:04:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/71838418.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定