-3

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"`
}
N D
  • 15
  • 5
  • I don't know what that means. Your question is very difficult to understand. – Jonathan Hall Dec 11 '18 at 12:17
  • `d` is not an array, it's a map and they are not one and the same in Go. Since you're using a map of interfaces getting any item from the map is gonna get ugly and the more nested the item is the more uglier the code gets. I would recommend you use struct types instead of maps or use a 3rd party lib that makes the traversing of a map easier. To see how ulgy it can get, consider the solution to get d.ask.assets[0].name: `d["ask"].(map[string]interface{})["assets"].([]interface{})[0].(map[string]interface{})["name"].(string)`. – mkopriva Dec 11 '18 at 13:21

1 Answers1

1

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.

Hasan
  • 1,243
  • 12
  • 27