Golang从比特币API查询的JSON返回了无效字符。

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

Golang json query from bitcoin api returns invalid character

问题

有些东西告诉我我对json的理解不正确。我试图从http://api.bitcoincharts.com/v1/trades.csv?symbol=rockUSD获取一些数据,但我的Unmarshal似乎无法读取json数据。我是一个刚入门的golang(以及json),我想知道如何跳过我所犯的错误字符错误。

我的错误:

顶级值后的无效字符','
panic: 顶级值后的无效字符','

我的代码:

package main

import ("fmt"
	"net/http"
	"io/ioutil"
	"encoding/json"
)

type Prices struct {
	Data string
}

func main() {
	url := "http://api.bitcoincharts.com/v1/trades.csv?symbol=rockUSD"
	httpresp, err := http.Get(url)
	if err != nil{
		fmt.Println(err)
		panic(err)
	}
	defer httpresp.Body.Close()
	htmldata, err := ioutil.ReadAll(httpresp.Body)
	if err != nil{
		fmt.Println(err)
		panic (err)
	}
	var jsonData []Prices
	err = json.Unmarshal([]byte(htmldata), &jsonData)
	if err != nil {
		fmt.Println(err)
		panic (err)
	}
	fmt.Println(jsonData)

}
英文:

Something tells me I'm not understanding json correctly. I'm trying to grab some data off http://api.bitcoincharts.com/v1/trades.csv?symbol=rockUSD, but my Unmarshal seems to not be able to read the json data. I'm a fresh beginner to golang (and json as well), and I'm wondering how I am able to skip that wrong character error I'm making.

My error:

invalid character ',' after top-level value
panic: invalid character ',' after top-level value

My code:

package main

import ("fmt"
	"net/http"
	"io/ioutil"
	"encoding/json"
)

type Prices struct {
	Data string
}

func main() {
	url := "http://api.bitcoincharts.com/v1/trades.csv?symbol=rockUSD"
	httpresp, err := http.Get(url)
	if err != nil{
		fmt.Println(err)
		panic(err)
	}
	defer httpresp.Body.Close()
	htmldata, err := ioutil.ReadAll(httpresp.Body)
	if err != nil{
		fmt.Println(err)
		panic (err)
	}
	var jsonData []Prices
	err = json.Unmarshal([]byte(htmldata), &jsonData)
	if err != nil {
		fmt.Println(err)
		panic (err)
	}
	fmt.Println(jsonData)

}

答案1

得分: 2

这根本不是 JSON 数据,你需要编写一个自定义解析器。

示例代码:

.........
data := readData(httpresp.Body)
.........

func readData(r io.Reader) (out [][3]float64) {
    br := bufio.NewScanner(r)
    for br.Scan() {
        parts := strings.Split(br.Text(), ",")
        if len(parts) != 3 {
            continue
        }
        var fparts [3]float64
        for i, p := range parts {
            // 忽略错误不是一个好主意,但这留给读者作为练习。
            fparts[i], _ = strconv.ParseFloat(p, 64)
        }
        out = append(out, fparts)
    }
    return
}

[kbd]playground/kbd

英文:

That is NOT json data at all, you'd have to write a custom parser.

Example:

.........
data := readData(httpresp.Body)
........

func readData(r io.Reader) (out [][3]float64) {
	br := bufio.NewScanner(r)
	for br.Scan() {
		parts := strings.Split(br.Text(), ",")
		if len(parts) != 3 {
			continue
		}
		var fparts [3]float64
		for i, p := range parts {
            // bad idea to ignore errors, but it's left as exercise for the reader.
			fparts[i], _ = strconv.ParseFloat(p, 64)
		}
		out = append(out, fparts)
	}
	return
}

<kbd>playground</kbd>

huangapple
  • 本文由 发表于 2015年8月22日 13:36:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/32152724.html
匿名

发表评论

匿名网友

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

确定