英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论