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

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

invalid character 'd' looking for beginning of value

问题

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

data = {
          'reason': 'invalid', 
          'dtype':   120, 
          'id':      708, 
          'scene':   'external_vender'
      }
send the data as json.dumps(data)

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

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

type Response struct {
    id       int64    `json:"id"`
    dtype    int32    `json:"type"`
    reason   string   `json:"reason"`
    scene    string   `json:"scene"`
}
var response Response
err := json.Unmarshal(Data, &response)
英文:

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

data = {
          'reason': 'invalid', 
          'dtype':   120, 
          'id':      708, 
          'scene':   'external_vender'
      }
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

type Response struct {
    id       int64     `json:"id"`
    dtype    int32     `json:"type"`
    reason   string    `json:"reason"`
    scene    string    `json:"scene"`
}
var response Response
err := json.Unmarshal(Data, &response)

答案1

得分: 1

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

将这段代码修改为:

type Response struct {
    Id       int64     `json:"id"`
    Dtype    int32     `json:"dtype"`
    Reason   string    `json:"reason"`
    Scene    string    `json:"scene"`
}

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

英文:

json.Unmarshall only works on fields with capital letters.

Change this:

type Response struct {
    id       int64     `json:”id”`
    dtype    int32     `json:”dtype”`
    reason   string    `json:”reason”`
    scene    string    `json:”scene”`
}

To this:

type Response struct {
    Id       int64     `json:"id"`
    Dtype    int32     `json:"dtype"`
    Reason   string    `json:"reason"`
    Scene    string    `json:"scene"`
}

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:

确定