S
S
s_pyanov2017-04-13 01:21:14
JSON
s_pyanov, 2017-04-13 01:21:14

How to process partially json structure in go?

Good day to all!
There is a task to parse json file. Problems in parsing There is no whole file, but there is a problem in parsing part of the file! View file:
-Head category
+ subcategory...
++subcategory parameters and more nested categories...
So, there are 15 head categories in the file, but at the moment I need only 10! How should I best handle the source file: create an array/map/whatever?
At the moment, to parse the entire file, the mem has the construction:

var str1 Menu

  err = json.Unmarshal(bs,&str1)
  if err!= nil{
    fmt.Println("Unmarshal error = ", err.Error())
  }

the Menu structure is quite complex (for me at least!!!):
type Menu struct {
  Catalog []struct {
    MainCategoryName string `json:"mainCategoryName"`
    MainCategorySlice []struct {
      CurrentCategoryName string `json:"currentCategoryName"`
      CurrentCategorySlice []struct {
        EndCategoryName string `json:"currentCategoryName"`
      } `json:"currentCategorySlice"`
    } `json:"mainCategorySlice"`
  } `json:"catalog"`
}

therefore, the problem arises (
How can I best process the incoming file if I later need to take ranges of head categories / a specific head category on request? Should I create an array with elements of the "head catalog" type? or are there some other processing mechanisms, just I don't know about them?
I apologize in advance if my question seems idiotic to someone! I'm just learning to program on my own and on specific tasks, so the questions are like this)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
gaxetasok, 2017-04-13
@s_pyanov

If you don't need to parse all fields - use "-" tag for unnecessary fields.
If you need to parse the structure flexibly - use RAW parsing (standard library functionality)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question