GoLang Json FXCM(外汇资本市场)

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

GoLang Json FXCM

问题

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

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
)

type Tick struct {
	Query string `json:"query"`
}

func main() {
	data := &Tick{Query: "https://ratesjson.fxcm.com/DataDisplayer?&callback=Tick"}
	buf := new(bytes.Buffer)
	enc := json.NewEncoder(buf)
	enc.SetEscapeHTML(true)
	_ = enc.Encode(data)
	fmt.Println(string(buf.Tick()))
	fmt.Println("Done")
}

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

package main

import (
	"log"
	"fmt"
	"net/http"
	"bytes"
	"io/ioutil"
	"github.com/pquerna/ffjson/ffjson"
)


type MsgRatesArray struct {
	RateQuote []MsgRateQuoteJson `json:"Rates"`
}
type MsgRateQuoteJson struct {
	SymbolName   string `json:"Symbol"`
	SymbolBid    int64  `json:"Bid"`
	SymbolAsk    int64  `json:"Ask"`
	SymbolSpread int64  `json:"Spread"`
	SymbolPT     string `json:"ProductType"`
}

var respBytes []byte

func main() {
	var msg MsgRatesArray
	response, err := http.Get("https://ratesjson.fxcm.com/DataDisplayer?&callback=Tick")
	if err != nil {
		log.Fatal(err)
	}
	defer response.Body.Close()
	respBytes, err := ioutil.ReadAll(response.Body)


	jsonBytes := respBytes[bytes.Index(respBytes, []byte("{")):bytes.LastIndex(respBytes, []byte("}"))+1]
	jsonString := string(jsonBytes)
	fmt.Println(jsonString)
	err = ffjson.Unmarshal(jsonBytes, &msg)
	if err != nil {
		panic(err)
	}
}
英文:

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

package main

import (
        "bytes"
        "encoding/json"
        "fmt"
)

type Tick struct {
        Query string `json:"query"`
}

func main() {
        data := &Tick{Query: "https://ratesjson.fxcm.com/DataDisplayer?&callback=Tick"}
        buf := new(bytes.Buffer)
        enc := json.NewEncoder(buf)
        enc.SetEscapeHTML(true)
        _ = enc.Encode(data)
        fmt.Println(string(buf.Tick()))
        fmt.Println("Done")
}

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

package main

import (
        "log"
        "fmt"
        "net/http"
        "bytes"
        "io/ioutil"
        "github.com/pquerna/ffjson/ffjson"
)


type MsgRatesArray struct {
        RateQuote []MsgRateQuoteJson `json:"Rates"`
}
type MsgRateQuoteJson struct {
        SymbolName string `json:"Symbol"`
        SymbolBid int64 `json:"Bid"`
        SymbolAsk int64 `json:"Ask"`
        SymbolSpread int64 `json:"Spread"`
        SymbolPT string `json:"ProductType"`
}

var respBytes []byte

func main() {
    var msg MsgRatesArray
    response,err := http.Get("https://ratesjson.fxcm.com/DataDisplayer?&callback=Tick")
    if err != nil {
        log.Fatal(err)
    }
    defer response.Body.Close()
    respBytes, err := ioutil.ReadAll(response.Body)


    jsonBytes := respBytes[bytes.Index(respBytes, []byte("{")):bytes.LastIndex(respBytes, []byte("}"))+1]
    jsonString := string(jsonBytes)
    fmt.Println(jsonString)
    err = ffjson.Unmarshal(jsonBytes, &msg)
    if err != nil {
        panic(err)
    }
}

答案1

得分: 0

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

response,err := http.Get("https://ratesjson.fxcm.com/DataDisplayer?&callback=Tick")
if err != nil {
    log.Fatal(err)
}
defer response.Body.Close()
respBytes, err := ioutil.ReadAll(response.Body)
jsonBytes := respBytes[bytes.Index(respBytes, []byte("{")):bytes.LastIndex(respBytes, []byte("}"))+1]
jsonString := string(jsonBytes)
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 }:

response,err := http.Get("https://ratesjson.fxcm.com/DataDisplayer?&callback=Tick")
if err != nil {
    log.Fatal(err)
}
defer response.Body.Close()
respBytes, err := ioutil.ReadAll(response.Body)
jsonBytes := respBytes[bytes.Index(respBytes, []byte("{")):bytes.LastIndex(respBytes, []byte("}"))+1]
jsonString := string(jsonBytes)
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:

确定