How to get the actual typeof(<JSON Array Element>).name ? #22817
-
Hi everyone,
The above outputs json2.Any as the type for each array element, how do I get the actual data type? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
I don't know why the post removed my line breaks from the code :-( |
Beta Was this translation helpful? Give feedback.
-
I would do this: import json
type Elem = string | f64
tx := [
Elem('ABC')
Elem(f64(123))
Elem(4.56)
]
assert json.encode(tx).str() == '["ABC",123,4.56]'
|
Beta Was this translation helpful? Give feedback.
Yes, the inverse would be:
The result of the above is:
And more work is needed to get apart the
ints
from thefloats
.Imho using a bag in json holding an array of diverse objects is sooner or later more difficult too handle.
I'll prefer to have an array with named elements, for instance: