简体   繁体   English

json:无法将字符串解组为 main.CaseReport 类型的 Go struct field.result.case_report

[英]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 .PrimaryContactCaseReport上实现解组器接口 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.

相关问题 json:无法将数字解组为 Go 结构字段。字符串类型的数量 - json: cannot unmarshal number into Go struct field .Amount of type string json:无法将字符串解组为main.test_struct类型的Go值 - json: cannot unmarshal string into Go value of type main.test_struct 松露测试:ProviderError: json: cannot unmarshal number into Go struct field params.fromBlock of type string - Truffle test: ProviderError: json: cannot unmarshal number into Go struct field params.fromBlock of type string Docker:b'json:无法将数字解组为Go结构字段LogConfig.Config,类型为string' - Docker: b'json: cannot unmarshal number into Go struct field LogConfig.Config of type string' 无法将字符串解组到Go结构字段中 - cannot unmarshal string into Go struct field json:无法将字符串解组为[] main.KVU类型的Go值 - json: cannot unmarshal string into Go value of type []main.KVU 无法将 JSON 字符串解组为 Go 中的结构 - Cannot Unmarshal JSON String to struct in Go 使用运行命令执行 SSM 文档失败 json:无法将数组解组为 Go 结构字段 RunScriptPluginInput.RunCommand 类型为 string\" - SSM document execution using run command failed json: cannot unmarshal array into Go struct field RunScriptPluginInput.RunCommand of type string\" 无法将字符串解组到类型模型的Go struct字段Article.article_type中。 - cannot unmarshal string into Go struct field Article.article_type of type models.ArticleType json:无法将数组解组为main类型的Go值 - json: cannot unmarshal array into Go value of type main.Posts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM