如何通过json将键,值对数组传递给结构的golang切片

I am writing a simple post api request. I am able to parse the JSON into golang structs upto the peername json object. I do not know the correct syntax to populate a golang slice of a struct by passing values through the JSON body of the api.

I am trying to parse JSON body sent through an api. This is the sample body request -

{  
   "type":"string",
   "name":"string",
   "organization":{  
      "orgID":"1",
      "orgName":"string",
      "peer":{  
         "peerID":"1",
         "peerName":"string"
      },
      "attributes":[
    ["slide0001.html", "Looking Ahead"],
    ["slide0008.html", "Forecast"],
    ["slide0021.html", "Summary"]
]
    }
} "peerName":"string"
          },
          "attributes":["name":"string":"value":true]
        }
    }

And this is my sample golang structs.

//Identity ...
type Identity struct {
    Type         string        `json:"type,omitempty"`
    Name         string        `json:"name,omitempty"`
    Organization *Organization `json:"organization,omitempty"`
}

//Organization ....
type Organization struct {
    OrgID      string      `json:"orgID,omitempty"`
    OrgName    string      `json:"orgName,omitempty"`
    Peer       *Peer       `json:"peer,omitempty"`
    Attributes *Attributes `json:"attributes"`
}

//Peer ...
type Peer struct {
    PeerID   string `json:"peerID,omitempty"`
    PeerName string `json:"peerName,omitempty"`
}

//Attributes ...
type Attributes []struct {
    Name  string `json:"name"`
    Value bool   `json:"value"`
}

Finally figured out the correct syntax. We have to pass an array of structs through JSON.

{  
   "type":"string",
   "name":"string",
   "organization":
   {  
      "orgID":"1",
      "orgName":"string",
      "peer":
      {  
         "peerID":"1",
         "peerName":"string"
      },
      "attributes":
      [
        {"slide0001.html": "Looking Ahead"},
        {"slide0008.html": "Forecast"},
        {"slide0021.html": "Summary"}
      ]
    }
}

you can do whatever you want in a UnmarshalJSON Function.

i made an example in playground. https://play.golang.org/p/WY6OCR8K3Co

you can get output: {A:[{Name:slide0001.html Value:Looking Ahead} {Name:slide0008.html Value:Forecast} {Name:slide0021.html Value:Summary}]}

var (
    jso = []byte(`
    {  
        "attributes":
        [
            {"slide0001.html": "Looking Ahead"},
            {"slide0008.html": "Forecast"},
            {"slide0021.html": "Summary"}
        ]
     }`)
)

type B struct {
    A As `json:"attributes"`
}

type As []A

type A struct {
    Name  string
    Value string
}

func (as *As) UnmarshalJSON(data []byte) error {
    var attr []interface{}
    if err := json.Unmarshal(data, &attr); err != nil {
        return err
    }
    if len(attr) > 0 {
        newAs := make([]A, len(attr))
        // i := 0
        for i, val := range attr {
            if kv, ok := val.(map[string]interface{}); ok && len(kv) > 0 {
                for k, v := range kv {
                    a := A{
                        Name:  k,
                        Value: v.(string),
                    }
                    newAs[i] = a
                    i++
                    break
                }
            }
        }
        *as = newAs
    }
    return nil
}