[英]json: cannot unmarshal string into Go struct field .result.case_report of type main.CaseReport
I need to unmarshal an API response that not always includes values for some of the structs defined inside the main struct.我需要解组一个 API 响应,该响应并不总是包含在主结构中定义的某些结构的值。 The main struct looks like this:主要结构如下所示:
type CaseResponse struct {
Result struct {
GroupList string `json:"group_list"`
UCopiedFrom string `json:"u_copied_from"`
UOfferVariant string `json:"u_offer_variant"`
SyncDriver string `json:"sync_driver"`
PrimaryContact *PrimaryContact `json:"primary_contact,omitempty"`
Entitlement string `json:"entitlement"`
CaseReport *CaseReport `json:"case_report,omitempty"`
ResolvedAt string `json:"resolved_at"`
} `json:"result"`
}
The internal structs for above json are defined as:上述 json 的内部结构定义为:
type PrimaryContact struct {
Link string `json:"link,omitempty"`
Value string `json:"value,omitempty"`
}
type CaseReport struct {
Link string `json:"link,omitempty"`
Value string `json:"value,omitempty"`
}
It is common for PrimaryContact and CaseReport to return empty, and when they do, Go throws error: json cannot unmarshal string into Go struct.result.case_report of type main.CaseReport PrimaryContact 和 CaseReport 返回空是很常见的,当它们返回空时,Go 会抛出错误:json cannot unmarshal string into Go struct.result.case_report of type main.CaseReport
I looked for possible fixes.我寻找可能的修复方法。 First I added the ",omitempty" to the values for the internal structs, but didn't fix the problem.首先,我将“,omitempty”添加到内部结构的值中,但没有解决问题。 Then added the pointers for PrimaryContact and CaseReport as found in some other postings, but I'm still hitting the same error when the response comes back with those fields with no data such as:然后添加了在其他一些帖子中找到的 PrimaryContact 和 CaseReport 的指针,但是当响应返回没有数据的字段时,我仍然遇到相同的错误,例如:
{
"result": {
"group_list": "",
"u_copied_from": "",
"u_offer_variant": "",
"sync_driver": "false",
"primary_contact":"",
"entitlement": "",
"case_report": "",
"resolved_at": ""
}
but works fine when primary_contact and case_report values come back as:但当 primary_contact 和 case_report 值返回时工作正常:
{
"result": {
"group_list": "",
"u_copied_from": "",
"u_offer_variant": "",
"sync_driver": "false",
"primary_contact": {
"link": "some link",
"value": "1e717ed013897b00f2345aa12244b003"
},
"entitlement": "",
"case_report": {
"link": "some link",
"value": "2b10fa5e1b1b19d45836ea89bd4bcb35"
},
"resolved_at": ""
}
How can the response be safely unmarshal when those fields have no value returned?当这些字段没有返回值时,如何安全地解组响应?
Implement the unmarshaler interface on PrimaryContact
and CaseReport
.在PrimaryContact
和CaseReport
上实现解组器接口。 Do nothing when the value is the empty string.当值为空字符串时不执行任何操作。
func (pc *PrimaryContact) UnmarshalJSON(p []byte) error {
if string(p) == `""` {
// empty string, do nothing
return nil
}
// Prevent recursion to this method by declaring a new
// type with same underlying type as PrimaryContact and
// no methods.
type x PrimaryContact
return json.Unmarshal(p, (*x)(pc))
}
https://go.dev/play/p/UHJobDw5RR2 https://go.dev/play/p/UHJobDw5RR2
Go throws error...去抛出错误...
The encoding/json package function returned the error. encoding/json 包函数返回错误。 Go does not have the throw feature. Go 没有 throw 特性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.