Go json.Unmarshall()适用于单个实体,但不适用于切片

Go json.Unmarshall()适用于单个实体,但不适用于切片,json,go,slice,Json,Go,Slice,我使用Go作为一个简单的http客户机。以下是我正在解组的实体: type Message struct { Id int64 Timestamp int64 Text string Author User LastEdited int64 } type User struct { Id int64 Name string } JSON中的单个实体如下所示: { "text": "hello, can you hear me?"

我使用Go作为一个简单的http客户机。以下是我正在解组的实体:

type Message struct {
    Id int64
    Timestamp int64
    Text string
    Author User
    LastEdited int64
}

type User struct {
    Id int64
    Name string
}
JSON中的单个实体如下所示:

{
    "text": "hello, can you hear me?",
    "timestamp": 1512964818565,
    "author": {
        "name": "andrea",
        "id": 3
    },
    "lastEdited": null,
    "id": 8
}
Go/json对解组单个实体没有问题:

var m Message

err = json.Unmarshal(body, &m)
if err != nil {
    printerr(err.Error())
}
println(m.Text)
但是,如果端点的返回是多个实体:

[
    {
        "text": "hello, can you hear me?",
        "timestamp": 1512964800981,
        "author": {
            "name": "eleven",
            "id": 4
        },
        "lastEdited": null,
        "id": 7
    }
]
我将对应的解组器更改为处理结构的切片,Go会抛出一个错误:

var m []Message

err = json.Unmarshal(body, &m)
if err != nil {
    printerr(err.Error()) // unexpected end of JSON input
}

for i := 0; i < len(m); i++ {
    println(m[i].Text)
}
var m[]消息
err=json.Unmarshal(body,&m)
如果出错!=零{
printerr(err.Error())//JSON输入意外结束
}
对于i:=0;i
提供什么?

对我来说很好(试穿),您从哪里获取有效负载数据?听起来像是截断了它

package main

import (
    "encoding/json"
    "fmt"
)

type Message struct {
    Id         int64
    Timestamp  int64
    Text       string
    Author     User
    LastEdited int64
}

type User struct {
    Id   int64
    Name string
}

func main() {
    body := []byte(`[
    {
        "text": "hello, can you hear me?",
        "timestamp": 1512964800981,
        "author": {
            "name": "eleven",
            "id": 4
        },
        "lastEdited": null,
        "id": 7
    }
]`)

    var m []Message

    err := json.Unmarshal(body, &m)
    if err != nil {
        fmt.Printf("error: %v") // unexpected end of JSON input
    }

    for i := 0; i < len(m); i++ {
        fmt.Println(m[i].Text)
    }
}
对我来说很好(试穿),你从哪里获得有效载荷数据?听起来像是截断了它

package main

import (
    "encoding/json"
    "fmt"
)

type Message struct {
    Id         int64
    Timestamp  int64
    Text       string
    Author     User
    LastEdited int64
}

type User struct {
    Id   int64
    Name string
}

func main() {
    body := []byte(`[
    {
        "text": "hello, can you hear me?",
        "timestamp": 1512964800981,
        "author": {
            "name": "eleven",
            "id": 4
        },
        "lastEdited": null,
        "id": 7
    }
]`)

    var m []Message

    err := json.Unmarshal(body, &m)
    if err != nil {
        fmt.Printf("error: %v") // unexpected end of JSON input
    }

    for i := 0; i < len(m); i++ {
        fmt.Println(m[i].Text)
    }
}

JSON格式不正确。它可能缺少结尾“]”或更多。显示用于读取HTTP响应的代码。不好意思说我只是请求了错误的URL并得到了空响应JSON格式不正确。它可能缺少结尾“]”或更多。显示用于读取HTTP响应的代码。不好意思说,我只是请求了错误的URL,得到了一个空响应