D
D
dubrovinn2018-12-12 11:54:54
go
dubrovinn, 2018-12-12 11:54:54

How can I create a struct for json object and get individual values ​​in golang?

I'm trying to write a function that will take an encoded string, send a request to a jsonrpc service, then I have to fetch certain data (ask:assets:interface) and create new variables
How can I create a struct for such an 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 that decodes a hex string into an object:
type order struct {
       ask `json:"ask"`   
       Cancomplete bool `json:"cancomplete"`   
       Candisable bool `json:"candisable"`   
       Complete bool `json:"complete"`   
       offer `json:"offer"`   
       Requiredfee float64 `json:"requiredfee"`
    }
     
    type ask struct {
       Ammount float64 `json:"ammount"`   
       assets `json:"assets"`
    }
     
    type offer struct {
       Ammount float64 `json:"ammount"`   
       assets `json:"assets"`
    }
     
    type assets struct {
       Assetref string `json:"assetref"`   
       Name string `json:"name"`   
       Qty float64 `json:"qty"`
    }
     
    func DecodeOrder(datahex string) (*order){
        var order *order   
        err := rpcClient.CallFor(&order, "decoderawexchange", datahex)
        if err != nil || order == nil {
           // handle error   }
        fmt.Print(order)
        return order
    }

Now this returns <nil>
Link to the rpc client in use - https://github.com/ybbus/jsonrpc

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Stupenkov, 2018-12-12
@alexstup

A hexadecimal string, this is apparently base64, translate into a string
Something like this:

package main

import (
  "encoding/json"
  "log"
)

type assetJSON struct {
  AssetRef string
  Name     string
  Qty      float64
}

type assetsJSON struct {
  Amount float64
  Assets []assetJSON
}

type objectJSON struct {
  Ask         assetsJSON
  CanComplete bool
  CanDisable  bool
  Complete    bool
  Offer       assetsJSON
  RequiredFee float64
}

func main() {
  jsonData := []byte(`{
      "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
  }`)
  obj := objectJSON{}
  if err := json.Unmarshal(jsonData, &obj); err != nil {
    log.Fatal(err)
  }
  log.Printf("%#v", obj)
}

https://play.golang.org/p/erXuDLOGzJs

D
dubrovinn, 2018-12-13
@dubrovinn

Everything worked out!

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question