在Go中的JSON请求中使用嵌套数组

How do I go about consuming the following json post in Go?

{
    "notificationType": "email",
    "client": "The CLient",
    "content": "Hellow World",
    "recipients": [
        {
            "address": "email1@example.com"
        },
        {
            "address": "email2@example.com"
        }
    ]
}

I've managed to get the string types but I really don't know enough about Go to deal with the recipients array.

My code looks like this:

package handlers

import (
    "net/http"
    "encoding/json"
    "notificationservice/src/validation"
    "io/ioutil"
    "fmt"
)

func EmailHandler(w http.ResponseWriter, r *http.Request) {
    b, err := ioutil.ReadAll(r.Body)
    defer r.Body.Close()
    if err != nil {
        http.Error(w, err.Error(), 500)
        return
    }

    var postData validation.EmailValidator
    err = json.Unmarshal(b, &postData)
    if err != nil {
        http.Error(w, err.Error(), 500)
        return
    }

    fmt.Println(postData.Client)
    fmt.Println(postData.Content)
}

My struct:

package validation

import "fmt"

type EmailValidator struct {
    Client  string `json:"client"`
    Content string `json:"content"`
    //Recipients []string `json:"recipients"`
}

func (validator * EmailValidator) ValidateEmail() (bool) {

    var required = []string {validator.Client, validator.Content, validator.Stuff}

    for _, param := range required {
        fmt.Println(param)
    }
    return true;
}

I've tried setting Recipients to []string and [][]string but I really don't know what I'm doing.

In PHP I would use the var_dump command to print out the entire object and debug step by step from there, but Go doesn't appear to have that functionality.

You can try something like this:

package main

import (
    "encoding/json"
    "fmt"
)

type Email struct {
    Adress string `json:"address"`
}

type EmailValidator struct {
    Client  string `json:"client"`
    Content string `json:"content"`
    Recipients []Email `json:"recipients"`
}

func main() {
    j := `{
    "notificationType": "email",
    "client": "The CLient",
    "content": "Hellow World",
    "recipients": [
        {
            "address": "email1@example.com"
        },
        {
            "address": "email2@example.com"
        }
    ]
    }`
    result := EmailValidator{}
    json.Unmarshal([]byte(j), &result)
    fmt.Printf("%+v", result) // something like var_dump in PHP
}

You can nest objects, and Unmarshal will handle the entire tree for you.

type Recipient struct {
    Address string `json:"address"`
}

type EmailValidator struct {
    Client  string `json:"client"`
    Content string `json:"content"`
    Recipients []Recipient `json:"recipients"`
}

The rest of your code looks good.

You need an array of "things with an address":

type EmailValidator struct {
    Client  string `json:"client"`
    Content string `json:"content"`
    Recipients []Recipient `json:"recipients"`
}


type Recipient struct {
    Address string `json:"address"`
}