Parse JSON array in golang using custom type
Feb 09, 2024 am 10:39 AMphp editor Baicao brings you an article about parsing JSON arrays in golang. In golang, we can use custom types to parse complex JSON arrays. This approach not only enables better understanding and processing of JSON data, but also provides a more flexible way of operation. This article will introduce in detail how to use custom types to parse JSON arrays, and give some practical example codes to help you better understand and apply this technique. Whether you are a beginner or an experienced developer, you can benefit a lot from this article, come and explore together!
Question content
I am trying to parse a json array in golang, the format is as follows:
2beef840f8f9d8bb724c7736cb14989For example, json should contain schemas for different tables. I have tried the following code but the schema returns empty:
package main import ( "encoding/json" "io" "log" "os" ) type ColumnType struct { Name string `json:"name"` Type string `json:"type"` } type Schema struct { Schema map[string][]ColumnType } func main() { mocksSchemas, _ := os.Open("parse_config/mock_schema.json") var schemas []Schema content, err := io.ReadAll(mocksSchemas) if err != nil { log.Fatal("Error when reading mock file: ", err) } err = json.Unmarshal(content, &schemas) if err != nil { log.Fatal("Error during Unmarshal(): ", err) } defer mocksSchemas.Close() }
What did i do wrong? I would be grateful if you could point out my mistake Thanks!
Workaround
To match the source/destination JSON you can use the following types:
type Schema map[string][]ColumnType
Alternatively, you can use something easier to use:
type Schema struct { Table string Columns []ColumnType } func (s Schema) MarshalJSON() ([]byte, error) { return json.Marshal(map[string][]ColumnType{ s.Table: s.Columns, }) } func (s *Schema) UnmarshalJSON(data []byte) error { var m map[string][]ColumnType if err := json.Unmarshal(data, &m); err != nil { return err } for k, v := range m { s.Table = k s.Columns = v break } return nil }
The above is the detailed content of Parse JSON array in golang using custom type. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Detailed explanation of sorting and filtering operations of JSON arrays in Java.

Get started quickly: JSON array merging and splitting techniques in Java.

How to convert JSON array to CSV in Java?

How can we merge two JSON arrays in Java?

How to deserialize JSON array to generic type of list in Java?

Beginner's Guide: FAQs on Manipulating JSON Arrays in Java.

How can we implement JSON array using streaming API in Java?

Parse JSON array in golang using custom type
