Home - development tool
 Collection

JSON to golang struct

JSON

operation





Result

type JSONData struct {
	Name string `json:"name"`
	Code int `json:"code"`
	List []List `json:"list"`
}
type List struct {
	Day int `json:"day"`
}

使用结构体

package main

import (
    "encoding/json"
    "fmt"
)

type JSONData struct {
    Name string `json:"name"`
    Code int    `json:"code"`
    List []List `json:"list"`
}
type List struct {
    Day int `json:"day"`
}

func main() {
    var jsonData JSONData
    jsonData.List = append(jsonData.List, List{1})
    jsonData.List = append(jsonData.List, List{2})

    respJson := JSONData{
        Code: 1,
        Name: "小芳",
        List: jsonData.List,
    }
    b, err := json.Marshal(respJson)
    if err != nil {
        fmt.Println("JSON ERR:", err)
    }
    fmt.Println(string(b))
}
                    

Tool description:

To convert golang into JSON, we need to define the structure first. If there are too many JSON fields, our workload will become larger and larger. Use this gadget to quickly generate the structure we need



Recommendation tool:

Tool label:

golang