[英]golang json unstructured data
我有一个 json(非结构化),想从 json 数据中检索每个键然后循环遍历键和值。 如果该值为 json(嵌套)或数组类型,则继续。
我找到了结构化 json 解析的示例,但无法得到这个。 检查了此代码,但无法获得完整的代码
err := json.Unmarshal([]byte(input), &customers)
样品 json:
{
"components": [
{
"key": "d1",
"components": [
{
"key": "custname",
"value": "Abraham",
"input": true,
"tableView": true
},
{
"key": "type",
"type": "radio",
"label": "Fisrt",
"values": [
{
"label": "Sole",
"value": "sole",
"shortcut": ""
},
{
"label": "Bata",
"value": "Bata",
"shortcut": ""
}
],
"validate": {
"required": true
},
"tableView": false
},
{
"key": "registeredField",
"value": "reg 111",
"input": true,
},
{
"key": "dirc",
"value": "abraham",
},
{
"key": "gst",
"value": "textfield",
"useLocaleSettings": false
},
{
"key": "pan",
"value": "AAAAA0000",
"useLocaleSettings": false
}
],
"collapsible": false
}
]
}
预期 output:
Key: custname Value: Abraham
Key: type Value: {
"label": "Sole",
"value": "sole",
"shortcut": ""
}, {
"label": "Bata",
"value": "Bata",
"shortcut": ""
}
Key: registeredField Value: reg 111
所以你有一个带有关键components
的 object ,这是一个组件。 这些组件中的每一个都可以有许多键。 首先要做的是评估组件可以拥有的所有可能字段并使用所述字段定义类型:
type Validation struct {
Required bool `json:"required"`
}
type Value struct {
Label string `json:"label"`
Value string `json:"value"`
Shortcut string `json:"shortcut"`
}
type Data struct {
Components []Data `json:"components,omitempty"`
Collapsable bool `json:"collapsable"`
Input bool `json:"input"`
Key string `json:"key"`
TableView bool `json:"tableView"`
Type string `json:"type"`
Value string `json:"value"`
UseLocaleSettings bool `json:"useLocaleSettings"`
Values []Value `json:"values,omitempty"`
Validate *Validation `json:"validate,omitempty"`
}
现在您只需获取输入并将其解组为Data
类型:
data := Data{}
if err := json.Unmarshal([]byte(input), &data); err != nil {
// handle error
fmt.Printf("Oops, something went wrong: %+v", err)
return
}
此时,我们将所有数据都放在了一个结构中,因此我们可以开始将其全部打印出来。 我们注意到的第一件事是Data
基本上是如何包含一个Data
类型的切片的。 将其全部打印出来的递归 function 是有意义的:
func PrintComponents(data []Data) {
for _, c := range data {
if len(c.Components) > 0 {
PrintComponents(c.Components) // recursive
continue // skip value of this component, remove this line if needed
}
val := c.Value // assign string value
if len(c.Values) > 0 {
// this component has a slice of values, not a single value
vals, err := json.MarshalIndent(c.Values, "", " ") // marshal with indent of 4 spaces, no prefix
if err != nil {
fmt.Printf("Oops, looks like we couldn't format something: %+v\n", err)
return // handle this
}
val = string(vals) // marshalled values as string
}
fmt.Printf("Key: %s Value: %s\n", c.Key, val) // print output
}
}
您可以稍微更改此 function 以传入每个递归级别的缩进参数,因此您可以打印出缩进块中的组件:
func PrintComponents(data []Data, indent string) {
for _, c := range data {
if len(c.Components) > 0 {
// print the key for this block of components
fmt.Printf("Component block: %s\n", c.Key)
PrintComponents(data, indent + " ") // current indent + 4 spaces
continue // we're done with this component
}
val := c.Value
if len(c.Values) > 0 {
vals, _ := json.MarshalIndent(c.Values, indent, " ") // pass in indent level here, and DON'T ignore the error, that's just for brevity
val = string(vals)
}
fmt.Printf("%sKey: %s Value: %s\n", indent, c.Key, val) // pass in indent
}
}
把它们放在一起,我们得到这个:
func main() {
data := Data{}
if err := json.Unmarshal(input, &data); err != nil {
fmt.Println(err.Error())
return
}
fmt.Println("Printing with simple recursive function")
// print all components, these could be nested, so let's use a recursive function
PrintComponents(data.Components)
fmt.Println("\n\nPrinting with indented recursion:")
PrintComponentsIndent(data.Components, "") // start with indent of 0
}
func PrintComponents(data []Data) {
for _, c := range data {
if len(c.Components) > 0 {
PrintComponents(c.Components) // recursive
continue // skip value of this component, remove this line if needed
}
val := c.Value // assign string value
if len(c.Values) > 0 {
// this component has a slice of values, not a single value
vals, err := json.MarshalIndent(c.Values, "", " ") // marshal with indent of 4 spaces, no prefix
if err != nil {
fmt.Printf("Oops, looks like we couldn't format something: %+v\n", err)
return // handle this
}
val = string(vals) // marshalled values as string
}
fmt.Printf("Key: %s Value: %s\n", c.Key, val) // print output
}
}
func PrintComponentsIndent(data []Data, indent string) {
for _, c := range data {
if len(c.Components) > 0 {
fmt.Printf("%sComponent block: %s\n", indent, c.Key)
PrintComponentsIndent(c.Components, indent + " ")
continue
}
val := c.Value
if len(c.Values) > 0 {
// this component has a slice of values, not a single value
vals, _ := json.MarshalIndent(c.Values, indent, " ")
val = string(vals) // marshalled values as string
}
fmt.Printf("%sKey: %s Value: %s\n", indent, c.Key, val) // print output
}
}
哪个输出:
Printing with simple recursive function
Key: custname Value: Abraham
Key: type Value: [
{
"label": "Sole",
"value": "sole",
"shortcut": ""
},
{
"label": "Bata",
"value": "Bata",
"shortcut": ""
}
]
Key: registeredField Value: reg 111
Key: dirc Value: abraham
Key: gst Value: textfield
Key: pan Value: AAAAA0000
Printing with indented recursion:
Component block: d1
Key: custname Value: Abraham
Key: type Value: [
{
"label": "Sole",
"value": "sole",
"shortcut": ""
},
{
"label": "Bata",
"value": "Bata",
"shortcut": ""
}
]
Key: registeredField Value: reg 111
Key: dirc Value: abraham
Key: gst Value: textfield
Key: pan Value: AAAAA0000
您想要的 output 不包括值切片的方括号。 嗯,这是一件很容易摆脱的事情。 方括号始终是字符串的第一个和最后一个字符,并且json.Marshal
返回一个字节切片 ( []byte
)。 删除第一个和最后一个字符很简单:
val = string(vals[1:len(vals)-2])
json.Marshal
返回的字节切片的子切片,从偏移量 1 开始(偏移量 0 的切割,即[
),并保留所有内容直到最后一个字符(偏移量len(vals)-2
)。 对于缩进的示例,这将给您留下一个空白行,其中包含未知数量的空格(缩进)。 您可以使用strings
package 修剪字符串的右侧:
// remove square brackets, trim trailing new-line and spaces
val = strings.TrimRight(string(vals[1:len(vals)-2]), "\n ")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.