0
获取值我有以下一段这就要求雅虎财经API来获取给定的股票代码股票值的代码。如何从JSON在golang
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
)
//Response structure
type Response struct {
Query struct {
Count int `json:"count"`
Created string `json:"created"`
Lang string `json:"lang"`
Results struct {
Quote []struct {
LastTradePriceOnly string `json:"LastTradePriceOnly"`
} `json:"quote"`
} `json:"results"`
} `json:"query"`
}
func main() {
var s Response
response, err := http.Get("http://query.yahooapis.com/v1/public/yql?q=select%20LastTradePriceOnly%20from%20yahoo.finance.quote%20where%20symbol%20in%20(%22AAPL%22,%22FB%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys")
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
} else {
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
json.Unmarshal([]byte(contents), &s)
fmt.Println(s.Query.Results.Quote)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
}
fmt.Printf("%s\n", string(contents))
}
}
fmt.Println(s.Query.Results.Quote)是给我一个多值序列,因为引用是对结构的阵列。对于例如:[{52.05},{} 114.25] 我应该如何在golang一个值拆呢? 例如:52.05 114.25
帮助受到高度赞赏。 感谢
你提到你是新的,所以注意一定,如果你知道这一点,但你可以使用:为_,Q:=范围s.Query.Results.Quote {} q.LastTradePriceOnly(作为一种简单的迭代)。同样,如果您LastTradePriceOnly类型float64(或FLOAT32),它会分析到数字形式。不这样做,如果它有时是为响应大卫非数字 –
感谢。我会考虑它:) –