Golang解组JSON列表

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?

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"`
}

playground example

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 "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)
}