从URL中读取JSON数据

huangapple go评论127阅读模式
英文:

Reading json from url

问题

这是一个用Go语言编写的代码片段,它从Google Spreadsheet中获取数据并进行解析。以下是翻译好的代码:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. )
  7. type GSSS struct {
  8. Feed GSSSfeed `json:"feed"`
  9. }
  10. type GSSSfeed struct {
  11. Version string `json:"version"`
  12. TITLE GSSTitle `json:"title"`
  13. // Entry []GSSSEntry `json:"entry"`
  14. }
  15. type GSSTitle struct {
  16. T string `json:"t"`
  17. }
  18. func main() {
  19. url01 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/1/public/values?alt=json"
  20. //url02 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/2/public/values?alt=json"
  21. //url03 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/3/public/values?alt=json"
  22. //url04 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/4/public/values?alt=json"
  23. //url05 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/5/public/values?alt=json"
  24. println("============= starting main =============")
  25. // res, err := http.Get("https://www.citibikenyc.com/stations/json")
  26. res, err := http.Get(url01)
  27. if err != nil {
  28. panic(err.Error())
  29. }
  30. var m GSSS
  31. json.NewDecoder(res.Body).Decode(&m)
  32. if err != nil {
  33. fmt.Println("Whoops...:", err)
  34. }
  35. fmt.Println("============ about to print m ============")
  36. fmt.Println(m.Feed.TITLE.T)
  37. fmt.Println("============ about to print m2 ============")
  38. fmt.Println(m.Feed)
  39. fmt.Println("============ about to print m3 ============")
  40. fmt.Println(m)
  41. fmt.Println("============ about to print m4 ============")
  42. }

这段代码创建了一个HTTP请求,从指定的Google Spreadsheet中获取数据,并将其解析为结构体。然后,它打印出解析后的数据。请注意,代码中的一些注释是被禁用的代码段。

英文:

The endpoint was created in Google SpreadSheet, which is here:

  1. url01 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/1/public/values?alt=json"

The Git repo is here... which is where I will keep the updated code as I fix it. My hunch is that I am screwing up the struct when I import the file. The code prints nothing. So, could I get some advice on what is going wrong please.

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. )
  7. type GSSS struct {
  8. Feed GSSSfeed `json:"feed"`
  9. }
  10. type GSSSfeed struct {
  11. Version string `json:"version"`
  12. TITLE GSSTitle `json:"title"`
  13. // Entry []GSSSEntry `json:"entry"`
  14. }
  15. type GSSTitle struct {
  16. T string `json:"t"`
  17. }
  18. func main() {
  19. url01 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/1/public/values?alt=json"
  20. //url02 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/2/public/values?alt=json"
  21. //url03 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/3/public/values?alt=json"
  22. //url04 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/4/public/values?alt=json"
  23. //url05 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/5/public/values?alt=json"
  24. println("============= starting main =============")
  25. // res, err := http.Get("https://www.citibikenyc.com/stations/json")
  26. res, err := http.Get(url01)
  27. if err != nil {
  28. panic(err.Error())
  29. }
  30. // body, err := ioutil.ReadAll(res.Body)
  31. // if err != nil {
  32. // panic(err.Error())
  33. // }
  34. var m GSSS
  35. // err := json.Unmarshal(body, &m)
  36. //json.NewDecoder(res.Body).Decode(&m)
  37. // json.NewDecoder([]byte(body)).Decode(&m)
  38. // json.NewDecoder([]byte(res.Body)).Decode(&m)
  39. json.NewDecoder(res.Body).Decode(&m)
  40. //err := json.Unmarshal([]byte(body), &m)
  41. if err != nil {
  42. fmt.Println("Whoops...:", err)
  43. }
  44. fmt.Println("============ about to print m ============")
  45. fmt.Println(m.Feed.TITLE.T)
  46. fmt.Println("============ about to print m2 ============")
  47. fmt.Println(m.Feed)
  48. fmt.Println("============ about to print m3 ============")
  49. fmt.Println(m)
  50. fmt.Println("============ about to print m4 ============")
  51. }

答案1

得分: 1

如已指出,你对GSSS的定义是错误的。

以下是修正后的代码:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. )
  7. type GSSS struct {
  8. Version string `json:"version"`
  9. Feed GSSSfeed `json:"feed"`
  10. }
  11. type GSSSfeed struct {
  12. TITLE GSSTitle `json:"title"`
  13. // Entry []GSSSEntry `json:"entry"`
  14. }
  15. type GSSTitle struct {
  16. T string `json:"$t"`
  17. }
  18. func main() {
  19. url01 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/1/public/values?alt=json"
  20. //url02 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/2/public/values?alt=json"
  21. //url03 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/3/public/values?alt=json"
  22. //url04 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/4/public/values?alt=json"
  23. //url05 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/5/public/values?alt=json"
  24. println("============= starting main =============")
  25. // res, err := http.Get("https://www.citibikenyc.com/stations/json")
  26. res, err := http.Get(url01)
  27. if err != nil {
  28. panic(err.Error())
  29. }
  30. var m GSSS
  31. json.NewDecoder(res.Body).Decode(&m)
  32. if err != nil {
  33. fmt.Println("Whoops...:", err)
  34. }
  35. fmt.Println("============ about to print m3 ============")
  36. fmt.Println(m)
  37. fmt.Println("============ about to print m4 ============")
  38. }

希望对你有帮助!

英文:

As already pointed out, your definition of GSSS is wrong.

This works a bit better:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. )
  7. type GSSS struct {
  8. Version string `json:"version"`
  9. Feed GSSSfeed `json:"feed"`
  10. }
  11. type GSSSfeed struct {
  12. TITLE GSSTitle `json:"title"`
  13. // Entry []GSSSEntry `json:"entry"`
  14. }
  15. type GSSTitle struct {
  16. T string `json:"$t"`
  17. }
  18. func main() {
  19. url01 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/1/public/values?alt=json"
  20. //url02 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/2/public/values?alt=json"
  21. //url03 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/3/public/values?alt=json"
  22. //url04 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/4/public/values?alt=json"
  23. //url05 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/5/public/values?alt=json"
  24. println("============= starting main =============")
  25. // res, err := http.Get("https://www.citibikenyc.com/stations/json")
  26. res, err := http.Get(url01)
  27. if err != nil {
  28. panic(err.Error())
  29. }
  30. // body, err := ioutil.ReadAll(res.Body)
  31. // if err != nil {
  32. // panic(err.Error())
  33. // }
  34. var m GSSS
  35. // err := json.Unmarshal(body, &m)
  36. //json.NewDecoder(res.Body).Decode(&m)
  37. // json.NewDecoder([]byte(body)).Decode(&m)
  38. // json.NewDecoder([]byte(res.Body)).Decode(&m)
  39. json.NewDecoder(res.Body).Decode(&m)
  40. //err := json.Unmarshal([]byte(body), &m)
  41. if err != nil {
  42. fmt.Println("Whoops...:", err)
  43. }
  44. fmt.Println("============ about to print m3 ============")
  45. fmt.Println(m)
  46. fmt.Println("============ about to print m4 ============")
  47. }

huangapple
  • 本文由 发表于 2017年6月28日 20:46:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/44802899.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定