package main
import "fmt"
import "encoding/json"
type trade struct {
Amount float64
Price float64
Tid uint64
Date uint64
Trade_side string
}
type res struct {
Status string
Trades []trade
}
func main() {
keysBody := []byte(`{"status":"ok","trades":[{"amount":1783.197,"price":11.64,"tid":1334222,"date":1501319002,"side":"sell","trade_side":"ask"},{"amount":1414.198,"price":11.64,"tid":1334223,"date":1501319002,"side":"sell","trade_side":"ask"}]}`)
//var keys []trade
var keys res
json.Unmarshal(keysBody, &keys)
fmt.Println(keys)
}
未知结构:
package main
import "fmt"
import "encoding/json"
func main() {
keysBody := []byte(`{"1":{"id":7,"last":"0.00000052","lowestAsk":"0.00000052","highestBid":"0.00000051","percentChange":"0.06122448","baseVolume":"82.45931512","quoteVolume":"161135661.58285543","isFrozen":"0","high24hr":"0.00000053","low24hr":"0.00000049"},"2":{"id":8,"last":"0.00005285","lowestAsk":"0.00005335","highestBid":"0.00005285","percentChange":"0.02841019","baseVolume":"8.86982292","quoteVolume":"169072.72178908","isFrozen":"0","high24hr":"0.00005453","low24hr":"0.00005051"}}`)
var n map[string]interface{}
err := json.Unmarshal(keysBody, &n)
if err != nil {
fmt.Println(err)
}
for k, v := range n {
fmt.Println(k, "------", v)
for k2, v2 := range v.(map[string]interface{}) {
fmt.Println(k2, "------", v2)
}
}
}
标签:none