# __ .______ __ __ .______ .___________. ______ ______ .___ ___. # | | | _ \ | | | | | _ \ | | / | / __ \ | \/ | # | | | |_) | | |__| | | |_) | `---| |----` | ,----'| | | | | \ / | # | | | ___/ | __ | | ___/ | | | | | | | | | |\/| | # | | | | | | | | | | | | __ | `----.| `--' | | | | | # |__| | _| |__| |__| | _| |__| (__) \______| \______/ |__| |__| # ""$o o$"" ""$o o$" o "$""""o "o $" o""" $" "$o "$o" $o " $ o$" "$o $$$o$$$$o$$$$ $" "oooo o "" ""$$$$$$$$""o"" oo oooo" "$$$$$$oo"oo$$$o" o$$$$oo" o$$$o "o$$$$$$$ "$ $$$$$$$$$oo o$$$$$$$$$o"$" $ $$$ $$$$$$ o$$$$$$ "$$o"o $ $$$$o $$$$$$ $$$$$$$ $$$$o"o $ $$$$$ $$$$$" "$$$$$ $$$$$$ $ $o""""" """" """ """"""$" $ o$$$$$"""$$$$$"$$$$$""$$$$$ooo"o $ o"$o $$$$$$$$oo$$$$$$$$o $$"" $ oo$ "$$$$$$$$$$$$$$$$$$$$" o" o $oo o$$$"$ $$o"o $$$$$$$"" "$$$$$$$ o$$ $$$$o IPHPT BUG o$$$$" $ $$$$ o "$$$$$oo o$$$$$$ "o$$$$ $ $$$$$ o$$"" $ $$$$$o" "$$$$$$$$$$$$$ o o$$$$$o$ "" $$ $$" $ $$$" o"o$$$$$$$$$$$$ " "$$$ $ $$o o$$ "o $$ " $$$$$$$$$$$"o "$$ $ $$$ $$$ oo$ $ o""$$""$$$o " $"o$o $$$o o$$$$ o$$$"o"$oo$$$$o" o $o $$$$$oo$ $$$$o $$$$ $$$$ $$$$" $ $$$$$"" $$ o$$$ """$$$$"o" "$$$o "$$$o $$$" o """ $ $$$oo $$$$o" $$ o$$$"o" """"$ o$$$ o$" $$$ $ "$"" o$"o"$$o$$$$ "$$"o" o$$ "$oo $ " $$o $ "oo$"o$$$"o$o"$$$$o" o" $$$ ""$o $$ $$$o "o$$o$"$$"$$o$$o$$"$$o" $$$ ""o $$$ ""$$$ $$$$$$ $$$$ $" $$$$ $$ $$$$ $$$$"$$$o$ $"" $$$ $$$$ "$$$ """ $$$$ $$"" "$$ oo$" $ooo $ "$$ go 定义复杂的json数据   -  叶落山城秋

go 定义复杂的json数据

go 定义复杂的json数据

前端需要一种复杂的json数据

类似于

    {
        "citys":[{
            "name":"哈哈",
                "value":[128.33165,45.451897,3],
                "symbolSize":2,
                "itemStyle":
                    {
                        "normal":
                            {
                                "color": "#F58158"
                            }
                    }
            }],
        "moveLines":[{
            "fromName":"哈哈",
            "toName":"北京",
            "coords":[[128.33165,45.451897,2],[116.407524,39.90403,-24]]
        }]
    };

以下是实现方法

package main

import (
	"fmt"
	"encoding/json"
)

type ItemStyle struct {
	Color string `json:"color"`
}
type City struct {
	Name       string        `json:"name"`
	Value      []interface{}        `json:"value"`
	SymbolSize int        `json:"symbolSize"`
	ItemStyle  map[string]ItemStyle        `json:"itemStyle"`
}

type MoveLines struct {
	FromName string 	`json:"fromName"`
	ToName string 		`json:"toName"`
	Coords []interface{}		`json:"coords"`
}

type All struct {
	Citys []interface{} `json:"citys"`
	MoveLines []interface{} `json:"moveLines"`
}

func doTest(gps1 float32,gps2 float32,name3 string)  ([]byte) {
	//alls := make([]All,1)

	//citys := make([]City, 1)
	//froms := make([]MoveLines, 1)

	city := &City{}
	city.Name = name3
	city.Value = make([]interface{}, 3)
	city.Value[0] = gps1
	city.Value[1] = gps2
	city.Value[2] = 3
	city.SymbolSize = 2
	city.ItemStyle = make(map[string]ItemStyle)
	city.ItemStyle["normal"] = ItemStyle{"#F58158"}

	from := &MoveLines{}
	from.FromName = name3
	from.ToName = "北京"
	from.Coords = make([]interface{},2)
	from.Coords[0] = [3]float32{gps1,gps2,2}
	from.Coords[1] = [3]float32{116.407526,39.90403,-24}

	//citys[0] = *city
	//froms[0] = *from

	all := &All{}
	all.Citys = make([]interface{},1)
	all.Citys[0] = *city
	all.MoveLines = make([]interface{},1)
	all.MoveLines[0] = *from

	//alls[0] = *all

	j, _ := json.Marshal(*all)
	return j
}

func main() {
	//alls := make([]All,1)
	//
	//citys := make([]City, 1)
	//froms := make([]MoveLines, 1)
	//
	//city := &City{}
	//city.Name = "延寿"
	//city.Value = make([]interface{}, 3)
	//city.Value[0] = 128.331644
	//city.Value[1] = 45.451897
	//city.Value[2] = 3
	//city.SymbolSize = 2
	//city.ItemStyle = make(map[string]ItemStyle)
	//city.ItemStyle["normal"] = ItemStyle{"#F58158"}
	//
	//from := &MoveLines{}
	//from.FromName = "延寿"
	//from.ToName = "北京"
	//from.Coords = make([]interface{},2)
	//from.Coords[0] = [3]float32{128.331644,45.451897,2}
	//from.Coords[1] = [3]float32{116.407526,39.90403,-24}
	//
	//citys[0] = *city
	//froms[0] = *from
	//
	//all := &All{}
	//all.Citys = make([]interface{},1)
	//all.Citys[0] = citys
	//all.MoveLines = make([]interface{},1)
	//all.MoveLines[0] = froms
	//
	//alls[0] = *all
	//
	//j, _ := json.Marshal(alls)
	//fmt.Printf("%s", j)
	test:=  doTest(128.331644,45.451897,"哈哈")
	fmt.Printf("%s", test)
}



欢迎转载,但请附上原文地址哦,尊重原创,谢谢大家 本文地址: https://www.iphpt.com/detail/84/
本站(PHP --> Golang)已重构,代码开源

当你能力不能满足你的野心的时候,你就该沉下心来学习