Как частично разобрать JSON с помощью Go?

У меня есть следующий 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
}

Заранее спасибо.

Посмотри, ясно ли сейчас @BasileStarynkevitch

Amanda Ferrari 10.09.2018 04:49

Нет, потому что JSON предназначен для анализа от корки до корки. Посмотрите еще раз на json.org и покажите минимальный воспроизводимый примерс кодом Go (который мы могли бы запустить на компьютере наш) в вашем вопросе. Возможно, вы используете «синтаксический анализ JSON» не так, как обычно.

Basile Starynkevitch 10.09.2018 04:50

@BasileStarynkevitch Я добавил пример.

Amanda Ferrari 10.09.2018 05:08
Как сделать HTTP-запрос в Javascript?
Как сделать HTTP-запрос в Javascript?
В JavaScript вы можете сделать HTTP-запрос, используя объект XMLHttpRequest или более новый API fetch. Вот пример для обоих методов:
10
3
5 088
3
Перейти к ответу Данный вопрос помечен как решенный

Ответы 3

Ответ принят как подходящий

Используйте 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.

Другие вопросы по теме