Table of Contents
Question content
Workaround
Home Backend Development Golang Parse JSON array in golang using custom type

Parse JSON array in golang using custom type

Feb 09, 2024 am 10:39 AM
json array

使用自定义类型解析 golang 中的 JSON 数组

php 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:

2beef840f8f9d8bb724c7736cb14989

For 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()
}
Copy after login

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
Copy after login

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
}
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of sorting and filtering operations of JSON arrays in Java. Detailed explanation of sorting and filtering operations of JSON arrays in Java. Sep 06, 2023 pm 03:22 PM

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

Get started quickly: JSON array merging and splitting techniques in Java. Get started quickly: JSON array merging and splitting techniques in Java. Sep 06, 2023 am 10:21 AM

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

How to convert JSON array to CSV in Java? How to convert JSON array to CSV in Java? Aug 21, 2023 pm 08:27 PM

How to convert JSON array to CSV in Java?

How can we merge two JSON arrays in Java? How can we merge two JSON arrays in Java? Aug 20, 2023 pm 11:05 PM

How can we merge two JSON arrays in Java?

How to deserialize JSON array to generic type of list in Java? How to deserialize JSON array to generic type of list in Java? Aug 20, 2023 pm 12:13 PM

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

Beginner's Guide: FAQs on Manipulating JSON Arrays in Java. Beginner's Guide: FAQs on Manipulating JSON Arrays in Java. Sep 06, 2023 am 11:22 AM

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

How can we implement JSON array using streaming API in Java? How can we implement JSON array using streaming API in Java? Sep 19, 2023 pm 06:01 PM

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

Parse JSON array in golang using custom type Parse JSON array in golang using custom type Feb 09, 2024 am 10:39 AM

Parse JSON array in golang using custom type

See all articles