英文:
Golang unmarshal json list
问题
我一直在努力解析一个基本的数组响应。
我的输入JSON中有一系列一致类型的结构。
[
{
"amount":"6.40000000",
"date":"1439165701",
"price":"350.26",
"tid":104159
},
{
"amount":"0.10025000",
"date":"1439162764",
"price":"351.03",
"tid":104150
}
]
我的结构体有一个嵌套的数组结构。
type TransactionResponse struct {
Transaction []Transaction
}
type Transaction struct {
Amount string `json:"amount"`
Date string `json:"date"`
Price string `json:"price"`
tid uint `json:"tid"`
}
解析JSON的函数:
func main() {
var transactions TransactionResponse
body, err := http.Get(url)
err = json.Unmarshal(body, &transactions)
}
我在哪里出错了?
英文:
I've been struggling to parse a basic array response.
My input JSON, has a list of consistent types of structures.
[
{
"amount":"6.40000000",
"date":"1439165701",
"price":"350.26",
"tid":104159
},
{
"amount":"0.10025000",
"date":"1439162764",
"price":"351.03",
"tid":104150
}
]
My struct has a nested array struct.
type TransactionResponse struct {
Transaction []Transaction
}
type Transaction struct {
Amount string `json:"amount"`
Date string `json:"date"`
Price string `json:"price"`
tid uint `json:"tid"`
}
Function to parse the json:
func main() {
var transactions TransactionResponse
body, err := http.Get(url)
err = json.Unmarshal(body, &transactions)
}
Where am I going wrong?
答案1
得分: 16
解码为交易切片:
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"`
}
英文:
Decode to a slice of transactions:
body, err := http.Get(url)
var transactions []Transaction
err = json.Unmarshal(body, &transactions)
Also, export all of the fields:
type Transaction struct {
Amount string `json:"amount"`
Date string `json:"date"`
Price string `json:"price"`
Tid uint `json:"tid"`
}
答案2
得分: 2
是的,这花了一些时间...
TransactionResponse 不是一个结构体类型。如果我将其改为 Transaction 数组,它将按预期工作。
package main
import (
"encoding/json"
"fmt"
)
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)
}
英文:
So yes, it took a while...
The TransactionResponse is not a struct type. If I make it a Transaction array, it works as it should.
package main
import (
"encoding/json"
"fmt"
)
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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论