登录
首页 >  Golang >  Go问答

在Golang中创建结构体并解组

来源:stackoverflow

时间:2024-03-15 16:27:27 394浏览 收藏

珍惜时间,勤奋学习!今天给大家带来《在Golang中创建结构体并解组》,正文内容主要涉及到等等,如果你正在学习Golang,或者是对Golang有疑问,欢迎大家关注我!后面我会持续更新相关内容的,希望都能帮到正在学习的大家!

问题内容

我是 golang 新手,我有一个问题。

我有 5 个使用 Json 的结构,但是 JSON 文件可以包含比我预先确定的结构更多的结构,但是...JSON 的结构满足我的编程中的结构(假设我有 5 个结构“struct1”) " 、6 个结构体 "struct 2" 、1 个结构体 "struct 3" 等等...)

我的问题是,我想创建一个函数,我获取 JSON 文件,读取它的结构,并将 JSON 文件的结构数量作为输出。

我想我可以使用map[string]interface{},但我不明白它。

我希望我已经解释清楚了 非常感谢你!


解决方案


如果没有示例 json 或结构,您所问的确切问题有点难以破译,特别是问题中的“输出结构数量”位,因为它可以用几种不同的方式解释。我将尽力回答我认为您最有可能提出的问题。

接口

首先,一些可能有用的基本 go 知识,但在 json 编组本身之外。 interface{} 类型看起来很特殊,但并不是像它最初出现的那样是硬连线关键字。 interface 关键字的作用是描述对象必须满足该接口的要求。因为 interface{} 没有任何要求,并且因为 go 中所有内容都会自动连接,所以所有内容都满足 interface{} 类型。

由于接口的这种实现,map[string]interface{}实际上是map[string]。这允许 json un/marshal 不关心映射值一侧的内容。这与 json 的格式完全一致,其中一侧有一个字符串键,另一侧的值可以是任何 json 数据类型。

基础 json 对象中有多少个不同的对象?

让我们取一个 example json

{
  "debug": "on",
  "window": {
    "title": "sample konfabulator widget",
    "name": "main_window",
    "width": 500,
    "height": 500
  },
  "image": {
    "src": "images/sun.png",
    "name": "sun1",
    "hoffset": 250,
    "voffset": 250,
    "alignment": "center"
  },
  "text": {
    "data": "click here",
    "size": 36,
    "style": "bold",
    "name": "text1",
    "hoffset": 250,
    "voffset": 100,
    "alignment": "center",
    "onmouseup": "sun1.opacity = (sun1.opacity / 100) * 90;"
  }
}

在这种情况下,问题的答案是四。调试、窗口、图像和文本。

确定数字的过程将是:

  1. 将 json 加载到字节数组中。
  2. 编组接口{}
  3. 使用类型开关确定类型(数组与对象等)。见this A Tour of Go page
    • 如果您知道类型,可以跳过此步骤
  4. 转换为所需类型
  5. 获取长度,或根据需要执行任何其他操作。
package main

import (
    "encoding/json"
    "fmt"
)

func main() {
    myjson := ``

    var outstruct *interface{}
    json.unmarshal([]byte(myjson), &outstruct)
    outmap := (*outstruct).(map[string]interface{})
    fmt.printf("num structs: %d", len(outmap))
}

Go Playground

存在多少个我没有结构的 json 对象?

这个答案与第一个答案非常相似,实际上是关于输出映射和结构的操作 考虑从第一个到第二个的几乎整个代码,我们假设您设置了以下结构

type image struct {
    name string
    //etc
}
type text struct {
    name string
    //etc
}
type window struct {
    name string
    //etc
}

type base struct {
    image  image
    window window
    text   text
}

在这种情况下,除了前面的步骤之外,您还必须 5. 将 json 编组为基础对象 6.遍历map[string]interface{},对于每个key 7. 确定键是否是基本结构中的对象之一

total := 0
for k, _ := range outmap {
    if k != "image" && k != "text" && k != "window" && k != "other" {
        total++
    }
}
fmt.printf("total unknown structs: %d\n", total)

我的结构有多少是空的?

最后一个问题也相当简单,可以通过检查映射中是否有给定输入键的值来完成,但为了完整起见,示例代码将 json 编组到一个结构中,并使用它。

  1. 将 json 编组到基础
  2. 对于base中的每个window、item、text、other,判断是否为空。
total = 0

if (Image{}) == outBase.Image {
    total++
}
if (Window{}) == outBase.Window {
    total++
}
if (Text{}) == outBase.Text {
    total++
}
if (Other{}) == outBase.Other {
    total++
}

fmt.Printf("Total empty structs: %d\n", total)

Go Playground

有关 golang json 的更多信息,请参阅 this go 博客文章。

今天关于《在Golang中创建结构体并解组》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

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