Answer the question
In order to leave comments, you need to log in
Golang how to parse Json array inside kson array?
Hello, I'm trying to parse a top-level json array that contains an array inside itself, but I end up with empty structures, please help
[{
"code": {
- Может называться как угодно "Serv": [{
"field1": 0,
"field2": 1399,
"field3": "Z",
"field4": 464,
"field5": "O",
"field6": 0000000000,
"field7": "ASSFSW",
"field8": "Ser",
"field9": "",
"field10": "C",
"field11": {},
"field12": "",
"field13": 1,
"field14": "",
"field15": "Stop",
"field16": "A",
"field17": 0000000000
}],
- Может называться как угодно "Serv1": [{
"field1": 0,
"field2": 1400,
"field3": "",
"field4": 464,
"field5": "O",
"field6": 0000000000,
"field7": "ASSFSWxxx",
"field8": "Ser",
"field9": "",
"field10": "C",
"field11": {
"field11.1": {
"field11.2": "N"
}
},
"field12": "",
"field13": 1,
"field14": "",
"field15": "Stop2",
"field16": "A",
"field17": 000000121
}]
},
"Code.field1": "0000000000",
"Code.field2": "05AUTOSP"
}
]
package main
import (
"bytes"
"encoding/json"
"fmt"
)
type Result struct {
Codes []Codes1
}
type Codes1 struct {
Codes []Codes
}
type Codes struct {
Filed1 string `json:"field1"`
Filed2 string `json:"field2"`
Filed3 string `json:"field3"`
}
const body = `[{
"code": {
"Serv": [{
"field1": 0,
"field2": 1399,
"field3": "Z",
"field4": 464,
"field5": "O",
"field6": 0000000000,
"field7": "ASSFSW",
"field8": "Ser",
"field9": "",
"field10": "C",
"field11": {},
"field12": "",
"field13": 1,
"field14": "",
"field15": "Stop",
"field16": "A",
"field17": 0000000000
}],
"Serv1": [{
"field1": 0,
"field2": 1400,
"field3": "",
"field4": 464,
"field5": "O",
"field6": 0000000000,
"field7": "ASSFSWxxx",
"field8": "Ser",
"field9": "",
"field10": "C",
"field11": {
"field11.1": {
"field11.2": "N"
}
},
"field12": "",
"field13": 1,
"field14": "",
"field15": "Stop2",
"field16": "A",
"field17": 000000121
}]
},
"Code.field1": "0000000000",
"Code.field2": "05AUTOSP"
}
]`
func main() {
resalt := make([]Result, 0)
decoder := json.NewDecoder(bytes.NewBufferString(body))
err := decoder.Decode(&resalt)
if err != nil {
panic(err)
}
fmt.Println(resalt[0])
}
[{[]} {[]} {[]} {[]} {[]} {[]} {[]} {[]} {[]} {[]} {[]} {[]} {[]} {[]}]
Answer the question
In order to leave comments, you need to log in
First, your json is invalid. If we correct it and correct the descriptions of the structures, we get
package main
import (
"encoding/json"
"fmt"
)
type Codes1 struct {
Codes map[string][]Codes `json:"code"`
}
type Codes struct {
Filed1 int `json:"field1"`
Filed2 int `json:"field2"`
Filed3 string `json:"field3"`
}
const body = `[
{
"code": {
"Serv": [
{
"field1": 0,
"field2": 1399,
"field3": "Z",
"field4": 464,
"field5": "O",
"field6": 0,
"field7": "ASSFSW",
"field8": "Ser",
"field9": "",
"field10": "C",
"field11": {},
"field12": "",
"field13": 1,
"field14": "",
"field15": "Stop",
"field16": "A",
"field17": 0
}
],
"Serv1": [
{
"field1": 0,
"field2": 1400,
"field3": "",
"field4": 464,
"field5": "O",
"field6": 0,
"field7": "ASSFSWxxx",
"field8": "Ser",
"field9": "",
"field10": "C",
"field11": {
"field11.1": {
"field11.2": "N"
}
},
"field12": "",
"field13": 1,
"field14": "",
"field15": "Stop2",
"field16": "A",
"field17": 121
}
]
},
"Code.field1": "0000000000",
"Code.field2": "05AUTOSP"
}
]`
func main() {
var result []Codes1
err := json.Unmarshal([]byte(body), &result)
if err != nil {
panic(err)
}
fmt.Println(result)
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question