小编Ale*_*giu的帖子

对嵌套结构使用自定义解组时,GoLang 结构无法正确解组

我们需要对嵌套在多个其他结构中的结构使用自定义解组器,而这些结构不需要自定义解组器。我们有很多与B下面定义的结构类似的结构(类似于嵌套A)。代码的输出是true false 0(预期的true false 2)。有任何想法吗?


Go Playground 示例在这里

package main

import (
    "fmt"
    "encoding/json"
)

type A struct {
    X bool `json:"x"`
    Y bool `json:"y"`
}

type B struct {
    A
    Z int `json:"z"`
}

func (a *A) UnmarshalJSON(bytes []byte) error {
    var aa struct {
        X string `json:"x"`
        Y string `json:"y"`
    }
    json.Unmarshal(bytes, &aa)

    a.X = aa.X == "123"
    a.Y = aa.Y == "abc"
    return nil
}

const myJSON …
Run Code Online (Sandbox Code Playgroud)

json struct nested go unmarshalling

4
推荐指数
1
解决办法
1878
查看次数

标签 统计

go ×1

json ×1

nested ×1

struct ×1

unmarshalling ×1