英文:
Reading json from url
问题
这是一个用Go语言编写的代码片段,它从Google Spreadsheet中获取数据并进行解析。以下是翻译好的代码:
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type GSSS struct {
Feed GSSSfeed `json:"feed"`
}
type GSSSfeed struct {
Version string `json:"version"`
TITLE GSSTitle `json:"title"`
// Entry []GSSSEntry `json:"entry"`
}
type GSSTitle struct {
T string `json:"t"`
}
func main() {
url01 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/1/public/values?alt=json"
//url02 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/2/public/values?alt=json"
//url03 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/3/public/values?alt=json"
//url04 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/4/public/values?alt=json"
//url05 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/5/public/values?alt=json"
println("============= starting main =============")
// res, err := http.Get("https://www.citibikenyc.com/stations/json")
res, err := http.Get(url01)
if err != nil {
panic(err.Error())
}
var m GSSS
json.NewDecoder(res.Body).Decode(&m)
if err != nil {
fmt.Println("Whoops...:", err)
}
fmt.Println("============ about to print m ============")
fmt.Println(m.Feed.TITLE.T)
fmt.Println("============ about to print m2 ============")
fmt.Println(m.Feed)
fmt.Println("============ about to print m3 ============")
fmt.Println(m)
fmt.Println("============ about to print m4 ============")
}
这段代码创建了一个HTTP请求,从指定的Google Spreadsheet中获取数据,并将其解析为结构体。然后,它打印出解析后的数据。请注意,代码中的一些注释是被禁用的代码段。
英文:
The endpoint was created in Google SpreadSheet, which is here:
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.
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type GSSS struct {
Feed GSSSfeed `json:"feed"`
}
type GSSSfeed struct {
Version string `json:"version"`
TITLE GSSTitle `json:"title"`
// Entry []GSSSEntry `json:"entry"`
}
type GSSTitle struct {
T string `json:"t"`
}
func main() {
url01 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/1/public/values?alt=json"
//url02 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/2/public/values?alt=json"
//url03 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/3/public/values?alt=json"
//url04 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/4/public/values?alt=json"
//url05 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/5/public/values?alt=json"
println("============= starting main =============")
// res, err := http.Get("https://www.citibikenyc.com/stations/json")
res, err := http.Get(url01)
if err != nil {
panic(err.Error())
}
// body, err := ioutil.ReadAll(res.Body)
// if err != nil {
// panic(err.Error())
// }
var m GSSS
// err := json.Unmarshal(body, &m)
//json.NewDecoder(res.Body).Decode(&m)
// json.NewDecoder([]byte(body)).Decode(&m)
// json.NewDecoder([]byte(res.Body)).Decode(&m)
json.NewDecoder(res.Body).Decode(&m)
//err := json.Unmarshal([]byte(body), &m)
if err != nil {
fmt.Println("Whoops...:", err)
}
fmt.Println("============ about to print m ============")
fmt.Println(m.Feed.TITLE.T)
fmt.Println("============ about to print m2 ============")
fmt.Println(m.Feed)
fmt.Println("============ about to print m3 ============")
fmt.Println(m)
fmt.Println("============ about to print m4 ============")
}
答案1
得分: 1
如已指出,你对GSSS
的定义是错误的。
以下是修正后的代码:
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type GSSS struct {
Version string `json:"version"`
Feed GSSSfeed `json:"feed"`
}
type GSSSfeed struct {
TITLE GSSTitle `json:"title"`
// Entry []GSSSEntry `json:"entry"`
}
type GSSTitle struct {
T string `json:"$t"`
}
func main() {
url01 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/1/public/values?alt=json"
//url02 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/2/public/values?alt=json"
//url03 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/3/public/values?alt=json"
//url04 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/4/public/values?alt=json"
//url05 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/5/public/values?alt=json"
println("============= starting main =============")
// res, err := http.Get("https://www.citibikenyc.com/stations/json")
res, err := http.Get(url01)
if err != nil {
panic(err.Error())
}
var m GSSS
json.NewDecoder(res.Body).Decode(&m)
if err != nil {
fmt.Println("Whoops...:", err)
}
fmt.Println("============ about to print m3 ============")
fmt.Println(m)
fmt.Println("============ about to print m4 ============")
}
希望对你有帮助!
英文:
As already pointed out, your definition of GSSS
is wrong.
This works a bit better:
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type GSSS struct {
Version string `json:"version"`
Feed GSSSfeed `json:"feed"`
}
type GSSSfeed struct {
TITLE GSSTitle `json:"title"`
// Entry []GSSSEntry `json:"entry"`
}
type GSSTitle struct {
T string `json:"$t"`
}
func main() {
url01 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/1/public/values?alt=json"
//url02 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/2/public/values?alt=json"
//url03 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/3/public/values?alt=json"
//url04 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/4/public/values?alt=json"
//url05 := "https://spreadsheets.google.com/feeds/list/168xdxefP3gGnrTGg2hwJoeRVfmbEuTB5plFTyd6I5Qo/5/public/values?alt=json"
println("============= starting main =============")
// res, err := http.Get("https://www.citibikenyc.com/stations/json")
res, err := http.Get(url01)
if err != nil {
panic(err.Error())
}
// body, err := ioutil.ReadAll(res.Body)
// if err != nil {
// panic(err.Error())
// }
var m GSSS
// err := json.Unmarshal(body, &m)
//json.NewDecoder(res.Body).Decode(&m)
// json.NewDecoder([]byte(body)).Decode(&m)
// json.NewDecoder([]byte(res.Body)).Decode(&m)
json.NewDecoder(res.Body).Decode(&m)
//err := json.Unmarshal([]byte(body), &m)
if err != nil {
fmt.Println("Whoops...:", err)
}
fmt.Println("============ about to print m3 ============")
fmt.Println(m)
fmt.Println("============ about to print m4 ============")
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论