N
N
neitri2018-11-01 14:01:52
go
neitri, 2018-11-01 14:01:52

How to unmarshal a json field if it can take a different type?

I use structure

type CustomColumn struct {
  ColName     string          `json:"ColName"`
  Description string          `json:"Description"`
  Type        string          `json:"Type"`
  Value       interface{}     `json:"Value"`
}

This structure is normally marshaled into a json file. The Value field can be of type sql.NullFloat64 sql.NullInt64 or NullTime.
How to properly unmarshal the received json?
PS I use a json file to store settings and results of work for re-launch.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Pavlyuk, 2018-11-01
@pav5000

In the same way, unmarshalling, you just have to pull the value from interface{} into the desired type.
https://tour.golang.org/methods/16
all numbers from Json are converted to float64 when unmarshaled in interface{}. If you need an int, you will have to turn it into an int later, after pulling it out of interface{}

T
TyzhSysAdmin, 2015-09-17
@M0NSTERC4T

He wrote everything to you.
Run apt-get updateit and see what errors there are, or the GPG signature fails verification somewhere or the repository
returns 404

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question