我試圖用golang讀取json文件,但是我收到了這個錯誤。 我已經檢查了幾乎所有關於它的問題,但仍然無法得到它。json:can not unmarshal array Go類型的值main.Posts
這裏的例子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)
}