海运的博客

Go语言解json

发布时间:July 30, 2017 // 分类: // No Comments

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

发表评论

分类
最新文章
最近回复
  • track: NAT 部分没处理,这块感觉有问题。
  • yu: 查到有用的资料很幸福!
  • boscotsang: 请问下服务器端如何控制权限让通过权限认证的客户端才能连接?现在这样只要有自编译的客户端程序都可...
  • yck932: 大侠,请问哪里有 tunnels 详细参考资料,谢谢
  • bubble: 现在linux 4.9以上的内核有个bbr的参数,用这个bbr比用锐速效果更好,可参考我的博文...
  • wang: openwrt 只有 tmp/etc/miniupnpd.conf 修改后又重置了,其他路径找...
  • wbq: 可以了 只是https透明代理如何做呢?
  • wbq: 你的这个配置需要浏览器设置代理ip+port, 现在假设不设置,只配置客户端网关为Nginx那...
  • wbq: 您好,想用Nginx做个透明代理,客户端机器配置下网关(即Nginx那台机器),客户端dns配...
  • ccc: 可以提供易调用openssl常用函数吗?