GoLang Json FXCM(外汇资本市场)

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

GoLang Json FXCM

问题

为什么这段代码没有输出字符串?有人知道如何让这段代码工作吗?

  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. )
  7. type Tick struct {
  8. Query string `json:"query"`
  9. }
  10. func main() {
  11. data := &Tick{Query: "https://ratesjson.fxcm.com/DataDisplayer?&callback=Tick"}
  12. buf := new(bytes.Buffer)
  13. enc := json.NewEncoder(buf)
  14. enc.SetEscapeHTML(true)
  15. _ = enc.Encode(data)
  16. fmt.Println(string(buf.Tick()))
  17. fmt.Println("Done")
  18. }

有人知道为什么这是无效的 JSON 或者为什么无法解析吗?或者指出如何修复让它工作?

  1. package main
  2. import (
  3. "log"
  4. "fmt"
  5. "net/http"
  6. "bytes"
  7. "io/ioutil"
  8. "github.com/pquerna/ffjson/ffjson"
  9. )
  10. type MsgRatesArray struct {
  11. RateQuote []MsgRateQuoteJson `json:"Rates"`
  12. }
  13. type MsgRateQuoteJson struct {
  14. SymbolName string `json:"Symbol"`
  15. SymbolBid int64 `json:"Bid"`
  16. SymbolAsk int64 `json:"Ask"`
  17. SymbolSpread int64 `json:"Spread"`
  18. SymbolPT string `json:"ProductType"`
  19. }
  20. var respBytes []byte
  21. func main() {
  22. var msg MsgRatesArray
  23. response, err := http.Get("https://ratesjson.fxcm.com/DataDisplayer?&callback=Tick")
  24. if err != nil {
  25. log.Fatal(err)
  26. }
  27. defer response.Body.Close()
  28. respBytes, err := ioutil.ReadAll(response.Body)
  29. jsonBytes := respBytes[bytes.Index(respBytes, []byte("{")):bytes.LastIndex(respBytes, []byte("}"))+1]
  30. jsonString := string(jsonBytes)
  31. fmt.Println(jsonString)
  32. err = ffjson.Unmarshal(jsonBytes, &msg)
  33. if err != nil {
  34. panic(err)
  35. }
  36. }
英文:

Why is this not dumping out the string? Anyone have any ideas how to get this code working?

  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. )
  7. type Tick struct {
  8. Query string `json:"query"`
  9. }
  10. func main() {
  11. data := &Tick{Query: "https://ratesjson.fxcm.com/DataDisplayer?&callback=Tick"}
  12. buf := new(bytes.Buffer)
  13. enc := json.NewEncoder(buf)
  14. enc.SetEscapeHTML(true)
  15. _ = enc.Encode(data)
  16. fmt.Println(string(buf.Tick()))
  17. fmt.Println("Done")
  18. }

Anyone know why this is invalid json or why this can not be parsed? Or point out the fix how to make this work?

  1. package main
  2. import (
  3. "log"
  4. "fmt"
  5. "net/http"
  6. "bytes"
  7. "io/ioutil"
  8. "github.com/pquerna/ffjson/ffjson"
  9. )
  10. type MsgRatesArray struct {
  11. RateQuote []MsgRateQuoteJson `json:"Rates"`
  12. }
  13. type MsgRateQuoteJson struct {
  14. SymbolName string `json:"Symbol"`
  15. SymbolBid int64 `json:"Bid"`
  16. SymbolAsk int64 `json:"Ask"`
  17. SymbolSpread int64 `json:"Spread"`
  18. SymbolPT string `json:"ProductType"`
  19. }
  20. var respBytes []byte
  21. func main() {
  22. var msg MsgRatesArray
  23. response,err := http.Get("https://ratesjson.fxcm.com/DataDisplayer?&callback=Tick")
  24. if err != nil {
  25. log.Fatal(err)
  26. }
  27. defer response.Body.Close()
  28. respBytes, err := ioutil.ReadAll(response.Body)
  29. jsonBytes := respBytes[bytes.Index(respBytes, []byte("{")):bytes.LastIndex(respBytes, []byte("}"))+1]
  30. jsonString := string(jsonBytes)
  31. fmt.Println(jsonString)
  32. err = ffjson.Unmarshal(jsonBytes, &msg)
  33. if err != nil {
  34. panic(err)
  35. }
  36. }

答案1

得分: 0

执行自己的http请求以获取json数据,然后剥离非json内容(即第一个{之前和最后一个}之后的所有内容):

  1. response,err := http.Get("https://ratesjson.fxcm.com/DataDisplayer?&callback=Tick")
  2. if err != nil {
  3. log.Fatal(err)
  4. }
  5. defer response.Body.Close()
  6. respBytes, err := ioutil.ReadAll(response.Body)
  7. jsonBytes := respBytes[bytes.Index(respBytes, []byte("{")):bytes.LastIndex(respBytes, []byte("}"))+1]
  8. jsonString := string(jsonBytes)
  9. fmt.Println(jsonString)

https://play.golang.org/p/JyibZ3g6UA

英文:

Do your own http request to get the json, then strip out the non json stuff (everything before the first { and after the last }:

  1. response,err := http.Get("https://ratesjson.fxcm.com/DataDisplayer?&callback=Tick")
  2. if err != nil {
  3. log.Fatal(err)
  4. }
  5. defer response.Body.Close()
  6. respBytes, err := ioutil.ReadAll(response.Body)
  7. jsonBytes := respBytes[bytes.Index(respBytes, []byte("{")):bytes.LastIndex(respBytes, []byte("}"))+1]
  8. jsonString := string(jsonBytes)
  9. fmt.Println(jsonString)

https://play.golang.org/p/JyibZ3g6UA

huangapple
  • 本文由 发表于 2017年4月28日 01:09:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/43664018.html
匿名

发表评论

匿名网友

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

确定