我有以下json:
{
"app": {
"name": "name-of-app",
"version" 1
},
"items": [
{
"type": "type-of-item",
"inputs": {
"input1": "value1"
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
items[0].inputs基于的变化items[0].type.
知道了,有没有办法让inputs字段保持字符串?我的想法是使用type调用正确的处理程序传递inputs,并在那里我将inputs使用正确的结构解析字符串.
例:
package main
import (
"fmt"
"encoding/json"
)
type Configuration struct {
App App `json:"app"`
Items []Item `json:"items"`
}
type App struct {
Name string `json:"name"`
Version int `json:"version"`
}
type Item struct {
Type string `json:"type"`
// What to put here to mantain the field a string so I can Unmarshal later?
// Inputs string
}
var myJson = `
{
"app": {
"name": "name-of-app",
"version": 1
},
"items": [
{
"type": "type-of-item",
"inputs": {
"input1": "value1"
}
}
]
}
`
func main() {
data := Configuration{}
json.Unmarshal([]byte(myJson), &data)
fmt.Println("done!", data)
// Loop through data.Items and use the type to define what to call, and pass inputs
// as argument
}
Run Code Online (Sandbox Code Playgroud)
先感谢您.
Cer*_*món 14
使用json.RawMessage获取inputs字段的原始JSON文本:
type Item struct {
Type string `json:"type"`
Inputs json.RawMessage
}
Run Code Online (Sandbox Code Playgroud)
像这样使用它:
var data Configuration
if err := json.Unmarshal([]byte(myJson), &data); err != nil {
// handle error
}
// Loop over items and unmarshal items.Inputs to Go type specific
// to each input type.
for _, item := range data.Items {
switch item.Type {
case "type-of-item":
var v struct{ Input1 string }
if err := json.Unmarshal(item.Inputs, &v); err != nil {
// handle error
}
fmt.Printf("%s has value %+v\n", item.Type, v)
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
In fairness Go would actually parse partially if you defined a partial struct. Quoting the documentation (https://blog.golang.org/json-and-go):
How does Unmarshal identify the fields in which to store the decoded data? For a given JSON key "Foo", Unmarshal will look through the destination struct's fields to find (in order of preference):
An exported field with a tag of "Foo" (see the Go spec for more on struct tags),
An exported field named "Foo", or
An exported field named "FOO" or "FoO" or some other case-insensitive match of "Foo".
Run Code Online (Sandbox Code Playgroud)
What happens when the structure of the JSON data doesn't exactly match the Go type?
b := []byte(`{"Name":"Bob","Food":"Pickle"}`)
var m Message
err := json.Unmarshal(b, &m)
Run Code Online (Sandbox Code Playgroud)
Unmarshal will decode only the fields that it can find in the destination type. In this case, only the Name field of m will be populated, and the Food field will be ignored. This behavior is particularly useful when you wish to pick only a few specific fields out of a large JSON blob. It also means that any unexported fields in the destination struct will be unaffected by Unmarshal.
| 归档时间: |
|
| 查看次数: |
1065 次 |
| 最近记录: |