代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | package main import ( "encoding/json" "fmt" ) type Data []struct { Source struct { Location struct { Lat float64 `json:"lat"` Lon float64 `json:"lon"` } `json:"location"` Time string `json:"time"` Speed float64 `json:"speed"` } `json:"_source"` Sort []int64 `json:"sort"` } func main() { location := []byte(`[ { "_source" : { "location" : { "lon" : 116.59, "lat" : 39.91 }, "time" : "2022-01-01T09:30:02", "speed" : 0.0 }, "sort" : [ 1 ] }, { "_source" : { "location" : { "lon" : 116.59, "lat" : 39.91 }, "time" : "2022-01-01T09:30:01", "speed" : 0.0 }, "sort" : [ 2 ] }, { "_source" : { "location" : { "lon" : 116.59, "lat" : 39.91 }, "time" : "2022-01-01T09:28:13", "speed" : 0.0 }, "sort" : [ 3 ] }]`) var dataObj Data json.Unmarshal(location, &dataObj) for _, value := range dataObj { sourceObj := value.Source locationObj := sourceObj.Location fmt.Println("时间: ", sourceObj.Time, "速度: ", sourceObj.Speed, ",纬度: ", locationObj.Lat, ",经度: ", locationObj.Lon) } } |