嵌套的JSON数据,使用单个结构体还是多个结构体?

huangapple go评论74阅读模式
英文:

Single struct or multiple for nested JSON?

问题

在Go语言中,处理嵌套的JSON数据时,创建相应的结构体有两种常见的方式。

第一种方式是使用单个结构体,将所有字段都嵌套在一个结构体中。例如:

type myStruct struct {
    Fields struct {
        Struct0 struct {
            Field0 string `json:"value"`
        } `json:"tag0"`
        Struct1 struct {
            Field0 int `json:"value"`
        } `json:"tag1"`
        Struct2 struct {
            Field0 int `json:"value"`
        } `json:"tag2"`
    } `json:"fields"`
}

第二种方式是使用多个结构体,每个嵌套的结构体对应一个字段。例如:

type myStruct struct {
    Fields struct {
        Struct0 `json:"tag0"`
        Struct1 `json:"tag1"`
        Struct2 `json:"tag2"`
    } `json:"fields"`
}

type Struct0 struct {
    Field0 string `json:"value"`
}

type Struct1 struct {
    Field0 int `json:"value"`
}

type Struct2 struct {
    Field0 int `json:"value"`
}

这两种方式各有优劣。使用单个结构体可以更紧凑地表示嵌套的JSON结构,但在访问字段时需要通过多层嵌套的结构体来获取。使用多个结构体可以更清晰地表示每个字段的结构,但会增加额外的结构体定义。

选择哪种方式取决于具体的需求和个人偏好。如果JSON结构比较简单且层级较浅,可以考虑使用单个结构体。如果JSON结构比较复杂或层级较深,使用多个结构体可以更好地组织代码和提高可读性。

英文:

I often have to work with nested JSON in Go and I'm wondering what's the most correct or idiomatic way to create the struct it's parsed into.

Single struct:

type myStruct struct {
    Fields struct {
	    Struct0 struct {
		    Field0 string `json:"value"`
	    } `json:"tag0"`
	    Struct1 struct {
		    Field0 int `json:"value"`
	    } `json:"tag1"`
	    Struct2 struct {
		    Field0 int `json:"value"`
	    } `json:"tag2"`
    } `json:"fields"`
}

Multiple structs:

type myStruct struct {
    Fields struct {
	    Struct0 `json:"tag0"`
	    Struct1 `json:"tag1"`
	    Struct2 `json:"tag2"`
    } `json:"fields"`
}

type Struct0 struct {
    Field0 string `json:"value"`
}

type Struct1 struct {
    Field0 int `json:"value"`
}

type Struct2 struct {
    Field0 int `json:"value"`
}

Is one way better than the other?

答案1

得分: 1

没有"更好"的方法,两种方法都是有效的,根据你的使用情况可能会有用。

单个结构体

  • 允许你在一个地方看到整个定义,有时可能更清晰
  • 结果是更简洁的定义代码

多个结构体

  • 允许你将"结构"(基本结构体)与对象(子结构体)分开
  • 可能使更改子结构体更简单
  • 如果需要,允许你重用一些子结构体

我认为最后一点(多个结构体允许你重用子结构体)是关键的区别,所以如果你的子结构体可能单独有用,使用多个结构体会更好。

英文:

There is no "better" way, both are valid and might be useful depending on your use case.

Single structs

  • Allow you to see the whole definition in one single place, which might be more clear at times
  • Result in more concise definition code

Multiple structs

  • Allow you to separate "structure" (the base struct) from objects (the sub-structs)
  • Might make it simpler to change sub-structs
  • Allow you to re-use some of the sub-structs if needed

I think that last point (multiple structs allowing you to re-use the sub-structs) is the key difference here, so if your sub-structs might be useful separately you would be better served by multiple structs.

答案2

得分: 0

我更喜欢单一的结构体,对我来说更清晰,但是有更多网络经验的人可能能够介入并说明行业的规范。

英文:

I prefer single structs, just seems cleaner to me, but someone with more network experience might be able to chime in as what is the industry norm.

答案3

得分: 0

这可能是非常主观的,但这取决于你如何使用数据以及你的JSON数据是否会随时间而变化。

如果你坚信你的JSON结构不会改变,那么单个结构应该是可以的。

如果你认为你的JSON结构可能会随时间而变化,特别是当你处理可能来自外部源的大型JSON对象时,使用多个结构可能更易于维护。

就一般情况而言,我个人不喜欢单个结构,我几乎总是发现随着时间的推移,它变得越来越难以维护。当我有多个版本的JSON结构,其中包含不同形式的数据时,我发现使用匿名字段要容易得多。

英文:

This can be really subjective, but it depends on how you're going to use the data and if/how your JSON data may change over time.

If you have a strong belief that your JSON structure will not change, the single struct should be fine.

If you believe that your JSON structure may change over time, especially if you're dealing with large JSON objects that might come from an external source, multiple structs can be more maintainable.

I personally don't like the single struct for a general use case, I almost always find that it gets harder to maintain over time. When I have multiple versions of the JSON struct with varying forms of data, I find it much easier to use anonymous fields.

答案4

得分: 0

如果你想要测试你的 JSON,那么使用多个结构体。

否则,你不能在函数外部初始化结构体(简洁地)以供表格测试使用。

https://stackoverflow.com/questions/24809235/initialize-a-nested-struct-in-golang 是一个处理这个问题的问答...

无论如何,这两种方法都有优缺点,但最终取决于你的使用情况:

多个结构体

  • 更容易测试
  • 模块化设计可重用

嵌套匿名结构体

  • 简洁
  • 所有内容都在一个地方 嵌套的JSON数据,使用单个结构体还是多个结构体?

实际上,除了测试之外,没有太大的区别。出于这个原因,我会选择使用多个显式结构体。

英文:

If you want to test your JSON then use multiple structs.

Otherwise you cannot initialize the struct outside of a function (concisely) for use in table tests.

https://stackoverflow.com/questions/24809235/initialize-a-nested-struct-in-golang is a question which deals with that...

In any case, there are pluses and minuses to both methods but ultimately it depends on your use case:

Multiple Structs

  • Easier to test
  • Modular design can be reused

Nested Anonymous Stucts

  • Concise
  • Everything in one place 嵌套的JSON数据,使用单个结构体还是多个结构体?

Effectively, not much of a difference aside from testing. I'd go with multiple explicit structs for that very reason.

huangapple
  • 本文由 发表于 2017年5月13日 07:30:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/43947733.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定