N
N
NOONE2019-04-23 17:34:26
go
NOONE, 2019-04-23 17:34:26

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"
}
]

The Serv and Serv1 fields can be named differently, and I need to get their structure from JSON
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])
}

As soon as I didn’t do it, I always get
[{[]} {[]} {[]} {[]} {[]} {[]} {[]} {[]} {[]} {[]} {[]} {[]} {[]} {[]}]

Give me an idea what's wrong? I just need to get all the JSON that comes in the code array

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pavlyuk, 2019-04-23
@Djam36

First, your json is invalid. If we correct it and correct the descriptions of the structures, we get

more or less like this
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)
}

https://play.golang.org/p/LB0TsM2a7Ai

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question