JSON-to-GO:将JSON转换成Go结构体定义 【GOLANG实现】
用法:
// 解析json元信息
meta := Parse(json)
// 根据元信息渲染输出结构体定义
// inline:true/false;结构体内联/分离输出
structDef := GenerateStruct(meta, inline)
安装:
go get github.com/AuroraV/json-to-go
使用样例
在线编辑
// You can edit this code!
// Click here and start typing.
package main
import (
"fmt"
"strings"
j2g "github.com/AuroraV/json-to-go"
)
func main() {
meta := j2g.Parse(`[{
"input_index": 100000001,
"components": [{
"primary_number": "1",
"a": 0.83
}, {
"a": 1.01,
"b": 1.28
}]
}]`)
structDef := j2g.GenerateStruct(meta, true)
structDef = j2g.GenerateStruct(meta, false)
fmt.Println(strings.Join(structDef, "\n----------------------------------------\n"))
// output:
/*
1. output [inline = true] :
type AutoGenerated []struct {
InputIndex int64 `json:"input_index"`
Components []struct {
PrimaryNumber string `json:"primary_number,omitempty"`
A float64 `json:"a"`
B float64 `json:"b,omitempty"`
} `json:"components"`
}
2. output [inline = false] :
type AutoGenerated []struct {
InputIndex int64 `json:"input_index"`
Components []Components `json:"components"`
}
----------------------------------------
type Components struct {
PrimaryNumber string `json:"primary_number,omitempty"`
A float64 `json:"a"`
B float64 `json:"b,omitempty"`
}
*/
}
License
This software is released under the MIT License, see LICENSE.