Hi all
I have the several structs in one format JSON
In one hard struct
type MainJSON []struct {
NestedInMain []struct {
Type string `json:"type"`
NestedInNested []struct {
Type string `json:"type"`
Points [][]float64 `json:"points"`
Label string `json:"label"`
} `json:"geometries"`
Time string `json:"time"`
} `json:"paths"`
Name string `json:"name"`
Description string `json:"description"`
Rating string `json:"rating"`
Prepared bool `json:"Prepared"`
}
I delimited her on several structs
type MainJson []struct {
NSIM NestedInMain
Name string `json:"name"`
Description string `json:"description"`
Rating string `json:"rating"`
Prepared bool `json:"Prepared"`
}
type NestedInMain []struct {
NIN NestedInNested
Type string `json:"type"`
Time string `json:"time"`
}
type NestedInNested []struct {
Type string `json:"type"`
Points [][]float64 `json:"points"`
Label string `json:"label"`
}
So when i do something like this
iap := MainJson{}
err := json.Unmarshal([]byte(test), &iap)
if err != nil {
fmt.Println(err)
return
}
log.Println(iap)
I can’t get access to data in first and second nested structures and they’s fields
(iap.NSIM)
or
(iap.NSIM.NIN.Label)
How do this correct?
Later i want get json from response.body
Thanks!!!
YongGopher:
NestedInMain
NestedInMain field has json:"paths"
tag you lose it
1 Like
I’m trying but this not work
This is mine json Example
[
{
"First": [],
"Nested": [
{
"Type": "type",
"Text": "text",
"NestedSecond": [
{
"Type": "typetwo",
"Name": "nametwo",
"NestedThird": [
[
46.203362,
2.5699142
],
[
46.202634,
2.5721897
]
]
}
]
}
],
"tags": [],
"name": "someName"
}
]
When i do this code
package main
import (
"encoding/json"
"log"
"os"
)
type Test struct {
First []interface{} `json:"First"`
NS []*Nested `json:"Nested"`
Tags []interface{} `json:"tags"`
Name string `json:"name"`
}
type Nested struct {
Type string `json:"Type"`
Text string `json:"Text"`
NST []*NestedSecond `json:"NestedSecond"`
}
type NestedSecond struct {
Type string `json:"Type"`
Name string `json:"Name"`
NestedThird [][]float64 `json:"NestedThird"`
}
var nT *[]Test
func main() {
file, _ := os.Open("test2.json")
decoder := json.NewDecoder(file)
err := decoder.Decode(&nT)
if err != nil {
log.Panic(err)
}
log.Println(nT)
}
showed me
&[{[] [0xc420092080] [] someName}]
But i can’t get access
log.Println(&nT.NS)
Returns me error
nT.NS undefined (type *[]Test has no field or method NS)
Finally I want to get NestedThird in two variables like a and b
a = 46.202634
b = 2.5699142
The error says that your type doesn’t have that field. Your type is a pointer to a slice of Test structures.
You want to access one field inside the Test structure… but which one? Your JSON gives you many, thus the slice (remove the pointer to the slice, please).
Probably you need to cycle over every Test structure contained in your JSON and extract the values:
nT[i].NS[j].NST[k].NestedThird[y]
Should give you a pair of a, b as you expect.
1 Like
Thanks!
But how do it more simple?
Because JSON return me array of pairs - which i want send in go slice in the same order
For example JSON
{
[
[1, 2],
[3, 4],
[5, 6]
]
}
NobbZ
(Norbert Melzer)
June 15, 2018, 11:32am
6
That JSON is not valid.
But if I simply remove the outer brackets, it’s a valid representation of [][2]int
.
2 Likes
system
(system)
Closed
September 13, 2018, 11:32am
7
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.