亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 GO 中解組嵌套的 JSON 對象

在 GO 中解組嵌套的 JSON 對象

Go
蝴蝶不菲 2022-10-10 19:21:38
我將所有對象共有的一些屬性組合到一個結構中。type Document struct {    ID        string    `json:"_id,omitempty"`    UpdatedAt time.Time `json:"updatedat"`    CreatedAt time.Time `json:"createdat"`}我還有一個地址結構,它不是文檔。type Address struct {    AddressLine string `json:"addressline,omitempty"`    City        string `json:"city,omitempty"`    Country     string `json:"country,omitempty"`    CityCode    int    `json:"citycode,omitempty"`}我的客戶結構是一個文檔。它還有一個地址屬性。type Customer struct {    Document `json:"document"`    Address  Address `json:"address"`    Name     string  `json:"name,omitempty"`    Email    string  `json:"email,omitempty"`    Valid    bool    `json:"valid,omitempty"`}來自 MongoDB 的 JSON 對象如下;[    {        "_id": "6186b4556971a9dbae117333",        "address": {            "addressline": "Foo Address",            "city": "Foo City",            "citycode": 0,            "country": "Foo Country"        },        "document": {            "createdat": "0001-01-01T03:00:00+03:00",            "updatedat": "0001-01-01T03:00:00+03:00"        },        "email": "[email protected]",        "name": "Foo Fooster",        "valid": false    }]我正在使用以下代碼來解組它。    var customerEntity Entities.Customer    json.Unmarshal(customerEntityBytes, &customerEntity)但我能得到的只是以下行。大多數字段為空。{{ 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC} {   0}   false}由于我認為這是由于混合嵌套結構,我創建了另一個客戶結構用于測試目的;import "time"type AutoGenerated []struct {    ID      string `json:"_id"`    Address struct {        Addressline string `json:"addressline"`        City        string `json:"city"`        Citycode    int    `json:"citycode"`        Country     string `json:"country"`    } `json:"address"`    Document struct {        Createdat time.Time `json:"createdat"`        Updatedat time.Time `json:"updatedat"`    } `json:"document"`}突然間,整個問題都解決了,我可以在填寫所有字段的情況下訪問它??傊?,我無法解組我想使用的客戶結構。我需要為此重寫 unmarshall 方法嗎?我還查看了覆蓋示例,但代碼非常主觀。我將在基類中進行的更改將導致我更改 unmarshall 方法。什么是干凈的方法?
查看完整描述

1 回答

?
森林海

TA貢獻2011條經驗 獲得超2個贊

始終檢查錯誤。


err = json.Unmarshal(customerEntityBytes, &customerEntity)

if err != nil {

    // json: cannot unmarshal array into Go value of type Entities.Customer

}

原因是,正如@mkopriva 指出的那樣-您的JSON 是一個數組-并且您正在解組為單個結構。修理:


 var customerEntity []Entities.Customer // use slice to capture JSON array

 err = json.Unmarshal(customerEntityBytes, &customerEntity)

 if err != nil { /* ... */ }

您當然可以使用您的自定義類型,但是_id通過將標簽嵌套在您的Document結構中會丟失標簽。要修復,請將其提升為Customer:


type Document struct {

    //ID        string    `json:"_id,omitempty"`

    UpdatedAt time.Time `json:"updatedat"`

    CreatedAt time.Time `json:"createdat"`

}


type Customer struct {

    ID       string `json:"_id,omitempty"`

    

    // ...

}

工作示例: https: //play.golang.org/p/EMcC0d1xOLf


查看完整回答
反對 回復 2022-10-10
  • 1 回答
  • 0 關注
  • 97 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號