Golang解析JSON列表

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

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

playground 示例

英文:

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

答案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)
}

huangapple
  • 本文由 发表于 2015年8月10日 09:29:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/31910482.html
匿名

发表评论

匿名网友

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

确定