收到EOF紧急错误

I'm trying to decode a json I get. Here's an example json I get:

{"response":"1","number":"1234","id":nil}

Here's my struct:

type AutoGenerated struct {
Response string  `json:"response"`
Number     string  `json:"number"`
ID         interface{}     `json:"id"`
}

I use the decode function in encode/json. What Am I getting wrong? ID has the chance to be both a string or a nil value.

Here's me exact error incase it helps.

panic: EOF

The JSON string you put here is invalid. You can find this code sample for reference.

If you're going to set the id field to nil, just don't put it in the JSON string.

package main

import (
    "encoding/json"
    "fmt"
    "io"
    "log"
    "strings"
)

type AutoGenerated struct {
    Response string      `json:"response"`
    Number   string      `json:"number"`
    ID       interface{} `json:"id"`
}

func main() {
    jsonStream := `
        { "response": "1", "number": "1234" }
        { "response": "1", "number": "1234", "id": "nil" }
    `
    decoder := json.NewDecoder(strings.NewReader(jsonStream))
    for {
        var m AutoGenerated
        if err := decoder.Decode(&m); err == io.EOF {
            break
        } else if err != nil {
            log.Fatal(err)
        }
        fmt.Println(m)
    }
}

The output of the program is:

{1 1234 <nil>}
{1 1234 nil}

Without you showing how you're doing it, I think the best answer is to show you how to do it.

package main

import (
    "fmt"
    "log"
    "encoding/json"
)

func main() {
    j := []byte(`{"response":"1","number":"1234","id":null}`)
    data := AutoGenerated{}
    err := json.Unmarshal(j, &data)
    if err != nil {
        log.Println(err.Error())
        return
    }
    fmt.Println(data)
}

type AutoGenerated struct {
    Response string      `json:"response"`
    Number   string      `json:"number"`
    ID       interface{} `json:"id"`
}