如何使用 Go 将 json 的深层嵌套部分转换为单个字符串
How to convert a deeply nested part of json into a single string with Go
我有一些 json 数据,其结构类似于以下内容:
{
"value1": "some value"
"value2": "some other value"
"value3": "another value"
"value4": {
"data":[
{
...more nested values here with a few more levels
}
]
}
}
我将如何布置结构,以便“value4”的所有数据通过 Go 作为单个字符串返回?
目前我正在使用 json.NewDecoder(r.Body).Decode(dataValues),其中 dataValues 是类似于以下的结构:
type DataValues struct {
Value1 string `json:"value1"`
Value2 string `json:"value2"`
Value3 string `json:"value3"`
// not sure how to define Value4
}
提前致谢!
How would I lay out the struct so that all the data for "value4" comes back as a single string with Go?
如果需要,您可以“延迟”解组并使用 encoding/json
中的 RawMessage 捕获要作为字符串处理的部分。
这是一个简单的solution。
我有一些 json 数据,其结构类似于以下内容:
{
"value1": "some value"
"value2": "some other value"
"value3": "another value"
"value4": {
"data":[
{
...more nested values here with a few more levels
}
]
}
}
我将如何布置结构,以便“value4”的所有数据通过 Go 作为单个字符串返回?
目前我正在使用 json.NewDecoder(r.Body).Decode(dataValues),其中 dataValues 是类似于以下的结构:
type DataValues struct {
Value1 string `json:"value1"`
Value2 string `json:"value2"`
Value3 string `json:"value3"`
// not sure how to define Value4
}
提前致谢!
How would I lay out the struct so that all the data for "value4" comes back as a single string with Go?
如果需要,您可以“延迟”解组并使用 encoding/json
中的 RawMessage 捕获要作为字符串处理的部分。
这是一个简单的solution。