解组数组中的单个元素

Is there a way to unmarshall JSON arrays into single objects in Go?

I have a json response from an endpoint:

{
  "results": [
    {
        "key": "value"
    }
  ]
}

I have a Go struct for the object inside the array:

type Object struct {
    Key string `json:"key"`
}

...and a struct for the response object:

type Response struct {
    Objects []Object `json:"results"`
}

results is an array of objects, but I know that the endpoint will only ever return an array with 1 object. Is there a way to unmarshall the data and avoid having reference the object by an index? I was hoping I could use something like json:"results[0]" as a field tag.

I'd prefer to be able to:

decoder.Decode(&response)
response.Object.Key

Rather than

decoder.Decode(&response)
response.Objects[0].Key

To does this you need to customize unmarshalling. An way is create a ResponseCustom like:

//Response json (customized) that match with Unmarshaler interface
type ResponseCustom struct {
    Object
}

func (r *ResponseCustom) UnmarshalJSON(b []byte) error{
    rsp := &Response{}
    err := json.Unmarshal(b, rsp)
    if err != nil {
        log.Fatalln("error:", err)
    } else {
        r.Object = rsp.Objects[0]
    }
    //
    return err
}

So you can use ResponseCustom instead of you Response for get Object value. Look:

func main() {
    //
    data := []byte(jsondata)
    resp := &ResponseCustom{}
    //
    json.Unmarshal(data, resp)
    //
    fmt.Println("My Object.value is: " + resp.Object.Key)
}

The result is:

    My Object.value is: value

In playground: https://play.golang.org/p/zo7wOSacA4w

Implement unmarshaler interface to convert array of object to object. Fetch the value for key and then unmarshal your json as

package main

import(
  "fmt"
  "encoding/json"
)

type Object struct {
  Key string `json:"key"`
}

func (obj *Object) UnmarshalJSON(data []byte) error {
  var v map[string]interface{}
  if err := json.Unmarshal(data, &v); err != nil {
      fmt.Printf("Error whilde decoding %v
", err)
      return err
  }
  obj.Key =  v["results"].(interface{}).([]interface{})[0].(interface{}).(map[string]interface{})["key"].(interface{}).(string)
  return nil
}


func main(){
  var obj Object
  data := []byte(`{
    "results": [
      {
          "key": "value"
      }
    ]
  }`)
  if err := json.Unmarshal(data, &obj); err!=nil{
    fmt.Println(err)
  }
  fmt.Printf("%#v", obj) 
}

Playground