json: 无法将数组解组为类型为 main.Posts 的 Go 值

huangapple go评论88阅读模式
英文:

json: cannot unmarshal array into Go value of type main.Posts

问题

我正在尝试使用Golang读取一个JSON文件,但是我遇到了这个错误。我已经查看了几乎所有相关的问题,但仍然无法解决。

这是示例JSON文件的链接:
https://jsonplaceholder.typicode.com/posts

以下是我的代码:

package main

import (
	"net/http"
	"log"
	"fmt"
	"io/ioutil"
	"encoding/json"
)

type Posts struct {
	Post []struct{
		UserId int `json:"userId"`
		ID int `json:"id"`
		Title string `json:"title"`
		Body string `json:"body"`
	}
}

func main (){
	resp, err := http.Get("https://jsonplaceholder.typicode.com/posts")

	if err != nil {
		log.Fatal(err)
	}

	content, _ := ioutil.ReadAll(resp.Body)

	var posts Posts

	parsed := json.Unmarshal([]byte(content), &posts)

	//fmt.Println(string(content))

	fmt.Println(parsed)
}

希望这可以帮助到你。

英文:

I'm trying to read a json file with golang but i'm getting this error.
I've checked almost every question about it but still couldnt get it.

Here's the example json file:
https://jsonplaceholder.typicode.com/posts

And my code:

package main

import (
	"net/http"
    "log"
	"fmt"
	"io/ioutil"
	"encoding/json"
)

type Posts struct {
    Post []struct{
	    UserId int `json:"userId"`
		ID int `json:"id"`
    	Title string `json:"title"`
	    Body string `json:"body"`
	}
}

func main (){
    resp, err := http.Get("https://jsonplaceholder.typicode.com/posts")

    if err != nil {
	    log.Fatal(err)
	}

    content, _ := ioutil.ReadAll(resp.Body)

    var posts Posts

    parsed := json.Unmarshal([]byte(content), &posts)

    //fmt.Println(string(content))

    fmt.Println(parsed)
  
}

答案1

得分: 6

Posts是一个Post结构体的数组,但你将Post定义为数组,这是你的第一个错误,另外Unmarshal函数不返回结果,只返回错误并填充给定的参数。

package main

import (
	"net/http"
	"log"
	"fmt"
	"io/ioutil"
	"encoding/json"
)

type Post struct {
	UserId int `json:"userId"`
	ID int `json:"id"`
	Title string `json:"title"`
	Body string `json:"body"`
}

type Posts []Post

func main() {
	resp, err := http.Get("https://jsonplaceholder.typicode.com/posts")

	if err != nil {
		log.Fatal(err)
	}

	content, _ := ioutil.ReadAll(resp.Body)

	var posts Posts

	err = json.Unmarshal(content, &posts)

	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(posts[0].Body)
}

以上是你要翻译的内容。

英文:

Posts is an array of Post struct but you defined Post as array it is your first mistake, also Unmarshal doesn't returns result it returns only error and fills given parameter.

package main

import (
	"net/http"
	"log"
	"fmt"
	"io/ioutil"
    "encoding/json"
)

type Post struct {
		UserId int `json:"userId"`
		ID int `json:"id"`
		Title string `json:"title"`
		Body string `json:"body"`
}

type Posts []Post


func main (){
    resp, err := http.Get("https://jsonplaceholder.typicode.com/posts")

    if err != nil {
	    log.Fatal(err)
    }

    content, _ := ioutil.ReadAll(resp.Body)

    var posts Posts

    err = json.Unmarshal(content, &posts)

    if err != nil {
	    log.Fatal(err)
    }


    fmt.Println(posts[0].Body)

}

答案2

得分: 4

这个JSON在根部是一个数组。你试图将其解组成一个包含数组字段的对象,因此出现了错误,即你传递了一个对象,而JSON是一个数组。你想要传递一个数组(或切片),如下所示:

type Post struct {
    UserId int `json:"userId"`
    ID int `json:"id"`
    Title string `json:"title"`
    Body string `json:"body"`
}

//...

var posts []Post
err := json.Unmarshal([]byte(content), &posts)

// 检查 err,对 posts 进行操作
英文:

That JSON is, at its root, an array. You're trying to unmarshal it into an object, which contains, as a field, an array - hence the error that you passed an object when the JSON is an array. You want to pass an array (or slice, really), as in:

type Post struct {
    UserId int `json:"userId"`
    ID int `json:"id"`
    Title string `json:"title"`
    Body string `json:"body"`
}

//...

var posts []Post
err := json.Unmarshal([]byte(content), &posts)

// Check err, do stuff with posts

huangapple
  • 本文由 发表于 2017年6月5日 23:02:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/44371865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定