doulin9679 2018-12-11 12:00
浏览 37

如何为对象创建结构并在Golang中获取值? [重复]

This question already has an answer here:

I'm trying to write a function that will take an encoded string, send a request to the jsonrpc service, then I need to choose a boring data (ask:assets:interface) and create a new variables

How can I create structure for object:

{
  "ask": {
  "amount": 0,
  "assets": [
    {
      "assetref": "74-266-27408",
      "name": "USD",
      "qty": 5000
    }
  ]
  },
  "cancomplete": true,
  "candisable": true,
  "complete": false,
  "offer": {
  "amount": 0,
  "assets": [
    {
      "assetref": "73-266-61482",
      "name": "BTC",
      "qty": 1
    }
  ]
  },
  "requiredfee": 0
}

And make a function wich decode hex string:

type Order struct {
    Ask []Ask `json:"ask"`
    Cancomplete bool `json:"cancomplete"`
    Candisable bool `json:"candisable"`
    Complete bool `json:"complete"`
    Offer []Offer `json:"offer"`
    Requiredfee int `json:"requiredfee"`
}

func DecodeOrder(datahex string) (Order)   {
    order, _ := rpcClient.Call("decoderawexchange", datahex)
    var d = make([]Order, 0, 100)
    err := order.GetObject(&d)
    if err != nil || d == nil {
        panic(err)
    }

    return d
}

Issue resolved using generated structure:

type Order struct {
    Ask struct {
        Amount float64 `json:"amount"`
        Assets []struct {
            Assetref string `json:"assetref"`
            Name     string `json:"name"`
            Qty      float64    `json:"qty"`
        } `json:"assets"`
    } `json:"ask"`
    Cancomplete bool `json:"cancomplete"`
    Candisable  bool `json:"candisable"`
    Complete    bool `json:"complete"`
    Offer       struct {
        Amount float64 `json:"amount"`
        Assets []struct {
            Assetref string `json:"assetref"`
            Name     string `json:"name"`
            Qty      float64    `json:"qty"`
        } `json:"assets"`
    } `json:"offer"`
    Requiredfee float64 `json:"requiredfee"`
}
</div>
  • 写回答

1条回答 默认 最新

  • duanshan1856 2018-12-11 12:33
    关注

    I did not get so much from your explanation but you can cycle through the arrays like this with range operation which returns index and value.

    for index, value := range !YourArray!{ //Do your stuff here }

    edit:

    Just to get single value from for loop with range you can use _ operator to neglect the unwanted variable.

        for _, u := range urls {
    

    Above index variable neglected,

        for i, _ := range urls {
    

    Value variable neglected.

    评论

报告相同问题?

悬赏问题

  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?