drhgzx4727 2019-09-13 10:50
浏览 190
已采纳

如何在Go中使用自定义属性类型进行JSON解组

In my project, I've defined structures so that get data from JSON. I tried to use json.Unmarshal() function. But it did not work for custom type attribute.

There is a structure like this:

type TestModel struct {
    ID   NullInt `json:"id"`
    Name string  `json:"name"`
}

In there, NullInt type was defined with implementations of MarshalJSON() and UnmarshalJSON() function:

// NullInt ...
type NullInt struct {
    Int   int
    Valid bool
}

// MarshalJSON ...
func (ni NullInt) MarshalJSON() ([]byte, error) {
    if !ni.Valid {
        return []byte("null"), nil
    }
    return json.Marshal(ni.Int)
}

// UnmarshalJSON ...
func (ni NullInt) UnmarshalJSON(b []byte) error {
    fmt.Println("UnmarshalJSON...")
    err := json.Unmarshal(b, &ni.Int)
    ni.Valid = (err == nil)
    fmt.Println("NullInt:", ni)
    return err
}

In main() function, I implemented:

func main() {
    model := new(TestModel)
    JSON := `{
        "id": 1,
        "name": "model" 
    }`
    json.Unmarshal([]byte(JSON), &model)
    fmt.Println("model.ID:", model.ID) 
}

In console, I got:

UnmarshalJSON...
NullInt: {1 true}
model.ID: {0 false}

As you can see, NullInt.UnmarshalJSON() was called and ni was what I expected but model.ID's value. What is the right way to implement UnmarshalJSON() function?

To addition, when I set: JSON := `{"name": "model"}` (without id), console just was: model.ID: {0 false} That means, the UnmarshalJSON() function wasn't called then I didn't get model.ID's value in right way.

  • 写回答

1条回答 默认 最新

  • dongzg2006 2019-09-13 10:54
    关注

    UnmarshalJSON() needs to modify the receiver, so you must use pointer receiver:

    func (ni *NullInt) UnmarshalJSON(b []byte) error {
        // ...
    }
    

    The receiver and all parameters are just copies, and if you don't use a pointer, you may only modify a copy which will be discarded once the method returns. If you use a pointer receiver, that is also just a copy, but the pointed value will be the same, hence you may modify the original (pointed) object.

    For consistency, also use pointer receiver for its other methods.

    With this change it works and outputs (try it on the Go Playground):

    UnmarshalJSON...
    NullInt: &{1 true}
    model.ID: {1 true}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀