2 回答
TA贡献2012条经验 获得超12个赞
解码为交易切片:
body, err := http.Get(url)
var transactions []Transaction
err = json.Unmarshal(body, &transactions)
此外,导出所有字段:
type Transaction struct {
Amount string `json:"amount"`
Date string `json:"date"`
Price string `json:"price"`
Tid uint `json:"tid"`
}
TA贡献1829条经验 获得超6个赞
所以是的,花了一段时间......
TransactionResponse 不是结构类型。如果我将其设置为 Transaction 数组,它就可以正常工作。
package main
import "fmt"
import "encoding/json"
var body = `[
{
"amount":"6.40000000",
"date":"1439165701",
"price":"350.26",
"tid":104159
},
{
"amount":"0.10025000",
"date":"1439162764",
"price":"351.03",
"tid":104150
}
]
`
type TransactionResponse []Transaction
type Transaction struct {
Amount string `json:"amount"`
Date string `json:"date"`
Price string `json:"price"`
tid uint `json:"tid"`
}
func main() {
var transactions TransactionResponse
err := json.Unmarshal([]byte(body), &transactions)
if err != nil {
panic(err)
}
fmt.Println(transactions)
}
- 2 回答
- 0 关注
- 158 浏览
添加回答
举报