У меня есть следующий json:
{
"app": {
"name": "name-of-app",
"version" 1
},
"items": [
{
"type": "type-of-item",
"inputs": {
"input1": "value1"
}
}
]
}
Замена 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
}
Заранее спасибо.
Нет, потому что JSON предназначен для анализа от корки до корки. Посмотрите еще раз на json.org и покажите минимальный воспроизводимый примерс кодом Go (который мы могли бы запустить на компьютере наш) в вашем вопросе. Возможно, вы используете «синтаксический анализ JSON» не так, как обычно.
@BasileStarynkevitch Я добавил пример.
Используйте json.RawMessage, чтобы получить необработанный текст JSON поля inputs
:
type Item struct {
Type string `json:"type"`
Inputs json.RawMessage
}
Используйте это так:
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)
}
}
Попробуйте gjson, супер просто, вам не нужно все размаршалировать. вы можете взять байты и вытащить определенное поле. https://github.com/tidwall/gjson
// Get string (has string, int, bool parsing)
someField := gjson.ParseBytes(b).Get("some_field.some_nested_field").Str
// Other types
v, ok := gjson.ParseBytes(b).Get("some_other_field").Value().(map[string]string)
Честно говоря, Go фактически выполнял бы частичный синтаксический анализ, если бы вы определили частичную структуру. Цитата из документации (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".
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)
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.
Посмотри, ясно ли сейчас @BasileStarynkevitch