登录
首页 >  Golang >  Go问答

将结构转换为映射的方法及使用特定字段作为键

来源:stackoverflow

时间:2024-02-22 10:18:23 346浏览 收藏

欢迎各位小伙伴来到golang学习网,相聚于此都是缘哈哈哈!今天我给大家带来《将结构转换为映射的方法及使用特定字段作为键》,这篇文章主要讲到等等知识,如果你对Golang相关的知识非常感兴趣或者正在自学,都可以关注我,我会持续更新相关文章!当然,有什么建议也欢迎在评论留言提出!一起学习!

问题内容

我正在尝试转换此结构

type news struct {
    id       string `json:"id"`
    title    string `json:"title"`
    author   string `json:"author"`
    date     string `json:"date"`
    escraped string `json:"escraped"`
    page     string `json:"page"`
    body     string `json:"body"`
    url      string `json:"url"`
}

对于以 id 为键的映射,今天当我编码此结构时,我在函数中返回以下 json

[
  {
    "id": "someid",
    "title": "something",
    "author": "something",
    "date": "something",
    "escraped": "something",
    "page": "https://something",
    "body": "something",
    "url": "https://something"
  }
]

但是现在我想更改它并使用id作为密钥,所以我想返回

[
  {
    "someId": {
      "title": "something",
      "author": "something",
      "date": "something",
      "escraped": "something",
      "page": "https://something",
      "body": "something",
      "url": "https://something"
    }
  }
]

我不太确定如何更改它以开始使用 id 作为键而不是作为常规字段,我尝试创建另一个地图,但失败了。


解决方案


我在这里做出一些假设:

  • 您在其他地方使用 news 结构,并且字段必须保持原样
  • 您尝试编码的数据是从 []news 的切片中读取的

您可以通过 3 个步骤完成此操作:

  1. 使用 omitemptyid 字段设为可选
  2. 使用 make() 定义并分配 map[string]news 类型的映射
  3. 迭代数据以填充 map

更新结构

type news struct {
    id       string `json:"id,omitempty"`
    title    string `json:"title"`
    author   string `json:"author"`
    date     string `json:"date"`
    escraped string `json:"escraped"`
    page     string `json:"page"`
    body     string `json:"body"`
    url      string `json:"url"`
}

创建地图

result := make(map[string]news)

填充地图

// transform a slice of news (called data) to a map (called result)
for _, entry := range data {
  result[entry.id] = news{
     title: entry.title,
     author: entry.author,
     date: entry.date,
     escraped: entry.escraped,
     page: entry.page,
     body: entry.body,
     url: entry.url,
  }
}
// encode the map
encoded, _ := json.marshal(result)

输出:

{"someid":{"title":"something","author":"something","date":"something","escraped":"something","page":"https://something","body":"something","url":"https://something"}}

请参阅此处的示例:https://play.golang.org/p/8z6M8HqCVgv

package main

import "fmt"
import "encoding/json"

type news struct {
  Id       string `json:"id"`
  Title    string `json:"title"`
  Author   string `json:"author"`
  Date     string `json:"date"`
  Escraped string `json:"escraped"`
  Page     string `json:"page"`
  Body     string `json:"body"`
  Url      string `json:"url"`
}

func main() {
  m := &news{Id: "222"}
  result := convert(m)
  str, _ := json.Marshal(result)
  fmt.Println(string(str))
}

func convert(m *news) interface{} {
  type tmp struct {
    *news
    Id string `json:"id,omitempty"`
  }
  t := &tmp{news: m}
  return map[string]interface{}{m.Id: t}
}

终于介绍完啦!小伙伴们,这篇关于《将结构转换为映射的方法及使用特定字段作为键》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>