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

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

如何在將json解組為結構時過濾掉重復項?

如何在將json解組為結構時過濾掉重復項?

Go
一只名叫tom的貓 2022-10-24 09:39:00
我有這個 json,我試圖將它解組到我的結構中。{  "clientMetrics": [    {      "clientId": 951231,      "customerData": {        "Process": [          "ABC"        ],        "Mat": [          "KKK"        ]      },      "legCustomer": [        8773      ]    },    {      "clientId": 1234,      "legCustomer": [        8789      ]    },    {      "clientId": 3435,      "otherIds": [        4,        32,        19      ],      "legCustomer": [        10005      ]    },    {      "clientId": 9981,      "catId": 8,      "legCustomer": [        13769      ]    },    {      "clientId": 12124,      "otherIds": [        33,        29      ],      "legCustomer": [        12815      ]    },    {      "clientId": 8712,      "customerData": {        "Process": [          "College"        ]      },      "legCustomer": [        951      ]    },    {      "clientId": 23214,      "legCustomer": [        12724,        12727      ]    },    {      "clientId": 119812,      "catId": 8,      "legCustomer": [        14519      ]    },    {      "clientId": 22315,      "otherIds": [        32      ],      "legCustomer": [        12725,        13993      ]    },    {      "clientId": 765121,      "catId": 8,      "legCustomer": [        14523      ]    }  ]}我使用此工具生成結構,如下所示 -type AutoGenerated struct {    ClientMetrics []ClientMetrics `json:"clientMetrics"`}type CustomerData struct {    Process []string `json:"Process"`    Mat     []string `json:"Mat"`}type ClientMetrics struct {    ClientID     int          `json:"clientId"`    CustomerData CustomerData `json:"customerData,omitempty"`    LegCustomer  []int        `json:"legCustomer"`    OtherIds     []int        `json:"otherIds,omitempty"`    CatID        int          `json:"catId,omitempty"`    CustomerData CustomerData `json:"customerData,omitempty"`}現在我的困惑是,我有很多字符串或 int 數組,那么如何過濾掉重復項?我相信 golang 中沒有設置數據類型,所以我怎樣才能在這里實現相同的目標?基本上,當我將 json 解組到我的結構中時,我需要確保根本不存在重復項。有什么辦法可以做到這一點?如果是,有人可以提供一個示例,如何為我的上述 json 實現這一點,以及我應該如何為此設計我的結構。
查看完整描述

1 回答

?
MMTTMM

TA貢獻1869條經驗 獲得超4個贊

理想情況下,您應該對這些數組進行后處理以刪除重復項。但是,您可以在解組期間使用帶有解組器的自定義類型來實現此目的:


type UniqueStrings []string


func (u *UniqueStrings) UnmarshalJSON(in []byte) error {

  var arr []string

  if err:=json.Unmarshal(in,arr); err!=nil {

     return err

  }

  *u=UniqueStrings(dedupStr(arr))

  return nil

}

在哪里


func dedupStr(in []string) []string {

   seen:=make(map[string]struct{})

   w:=0

   for i:=range in {

      if _,s:=seen[in[i]]; !s {

         seen[in[i]]=struct{}{}

         in[w]=in[i]

         w++

      } 

   }

   return in[:w]

}

您可以對 s 使用類似的方法[]int。


您在結構中使用自定義類型:


type CustomerData struct {

    Process UniqueStrings `json:"Process"`

    Mat     UniqueStrings `json:"Mat"`

}


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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