package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
// "time"
)
// RawData
type RawData struct {
Ret int `json:"ret"`
Data string `json:"data"`
}
// TotalData
type TotalData struct {
Confirm int `json:"confirm"`
Dead int `json:"dead"`
Heal int `json:"heal"`
Suspect int `json:"suspect"`
}
// ProvinceTree
type ProvinceTree struct {
Name string `json:"name"`
Children interface{} `json:"children"`
Total TotalData `json:"total"`
}
// CountryTree
type CountryTree struct {
Name string `json:"name"`
Children []ProvinceTree `json:"children"`
Total TotalData `json:"total"`
}
// AreaTree
type AreaTree struct {
Name string `json:"name"`
Children []CountryTree `json:"children"`
}
// ResData
type ResData struct {
ChinaTotal interface{} `json:"chinaTotal"`
ChinaAdd interface{} `json:"chinaAdd"`
LastUpdateTime interface{} `json:"lastUpdateTime"`
AreaTree []AreaTree `json:"areaTree"`
}
// DetailData
type DetailData struct {
Ret int `json:"ret"`
Data ResData `json:"data"`
}
// ResProvinceMap
type ResProvinceData struct {
Name string `json:"name"`
Value int `json:"value"`
}
// MapData
type MapData struct {
Ret int `json:"ret"`
Data []ResProvinceData `json:"data"`
}
// todo func GetJson(xc , ty int) {
func main() {
// todo for {
// time.Sleep()
url := "腾讯 h5 disease url"
dataStr := Download(url)
// raw data
rawData := &RawData{}
err := json.Unmarshal([]byte(dataStr), rawData)
if err != nil {
return
}
resData := &ResData{}
err = json.Unmarshal([]byte(rawData.Data), resData)
if err != nil {
return
}
// detail data
detailData := DetailData{
1,
*resData,
}
WriteFile(detailData, "test.json")
// map data
countryCN := AreaTree{}
for _, country := range resData.AreaTree {
if country.Name == "中国" {
countryCN = country
}
}
ProvinceDataList := make([]ResProvinceData, 0, len(countryCN.Children))
var tmpProvinceData ResProvinceData
for _, province := range countryCN.Children {
tmpProvinceData.Name = province.Name
tmpProvinceData.Value = province.Total.Confirm
ProvinceDataList = append(ProvinceDataList, tmpProvinceData)
}
mapData := &MapData{
1,
ProvinceDataList,
}
WriteFile(mapData, "test2.json")
}
func WriteFile(data interface{}, fileName string) {
// 创建文件
filePtr, err := os.Create(fileName)
if err != nil {
fmt.Println("Create file failed", err.Error())
return
}
defer filePtr.Close()
// 创建Json编码器
encoder := json.NewEncoder(filePtr)
err = encoder.Encode(data)
if err != nil {
fmt.Println("Encoder failed", err.Error())
} else {
fmt.Println("Encoder success")
}
}
func Download(url string) (res string){
client := &http.Client{}
req, _ := http.NewRequest("GET", url, nil)
// 自定义Header
req.Header.Set("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)")
resp, err := client.Do(req)
if err != nil {
// todo xlog.ErrorC(xc, "Download() get error:%v", err)
return
}
//函数结束后关闭相关链接
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
// todoxlog.ErrorC(xc, "Download() get while read. error:%v", err)
return
}
res = string(body)
return
}