英文:
Issue with unmarshaling json
问题
这是我尝试做的简化版本:
type example struct {
Topics []struct {
Id int64 `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
Tags []string `json:"tags"`
Search_phrases []string `json:"search_phrases"`
} `json:"topics"`
}
func main() {
body := []byte(
`
{
"topics": [{
"id": 35436,
"title": "How to Disassemble the ED209",
"body": "Carefully with very large pliers",
"tags": ["danger"],
"search_phrases": ["red", "yellow"]
}]
}
`)
var topics example
err := json.Unmarshal(body, &topics)
if err != nil {
fmt.Println(err)
}
/*
for _, topic := range topics.Topics {
//doSomething
}
*/
}
这看起来对我来说没问题,但是我得到了:
"json: 无法将对象解组为类型为 []main.example 的 Go 值"
我可能只是漏掉了一些小细节,但是我目前找不到它。
英文:
Here's a simplified version what I'm trying to do:
type example struct {
Topics []struct {
Id int64 `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
Tags []string `json:"tags"`
Search_phrases []string `json:"search_phrases"`
} `json:"topics"`
}
func main() {
body := []byte(
`
{
"topics": [{
"id": 35436,
"title": "How to Disassemble the ED209",
"body": "Carefully with very large pliers",
"tags": ["danger"],
"search_phrases": ["red", "yellow"]
}]
}
`)
var topics []example
err := json.Unmarshal(body, &topics)
if err != nil {
fmt.Println(err)
}
/*
for _, topics := range topics {
//doSomething
}
*/
}
This looks fine to me, but I am getting:
> "json: cannot unmarshal object into Go value of type []main.example"
I'm probably just missing something small, but I can't seem to find it at the moment.
答案1
得分: 2
你正在尝试将单个元素解组成一个列表。
请改为使用单个元素。
package main
import (
"encoding/json"
"fmt"
)
type example struct {
Topics []struct {
Id int64 `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
Tags []string `json:"tags"`
Search_phrases []string `json:"search_phrases"`
} `json:"topics"`
}
func main() {
body := []byte(
`
{
"topics": [{
"id": 35436,
"title": "How to Disassemble the ED209",
"body": "Carefully with very large pliers",
"tags": ["danger"],
"search_phrases": ["red", "yellow"]
}]
}
`)
var topic example
err := json.Unmarshal(body, &topic)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%#v\n", topic)
/*
for _, topics := range topics {
//doSomething
}
*/
}
链接:http://play.golang.org/p/g4Fblu_YRP
英文:
You are trying to unmarshal a single element into a list.
Use a single element instead.
http://play.golang.org/p/g4Fblu_YRP
package main
import (
"encoding/json"
"fmt"
)
type example struct {
Topics []struct {
Id int64 `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
Tags []string `json:"tags"`
Search_phrases []string `json:"search_phrases"`
} `json:"topics"`
}
func main() {
body := []byte(
`
{
"topics": [{
"id": 35436,
"title": "How to Disassemble the ED209",
"body": "Carefully with very large pliers",
"tags": ["danger"],
"search_phrases": ["red", "yellow"]
}]
}
`)
var topic example
err := json.Unmarshal(body, &topic)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%#v\n", topic)
/*
for _, topics := range topics {
//doSomething
}
*/
}
答案2
得分: 2
错误实际上解释了问题,你试图使用[]example
,而你的 JSON 示例是一个对象而不是一个数组,只需将其更改为:
var ex example
err := json.Unmarshal(body, &ex)
if err != nil {
fmt.Println(err)
}
fmt.Println(ex.Topics)
英文:
The error really explains the problem, you're trying to use []example
where your json example is an object not an array, simply change it to:
var ex example
err := json.Unmarshal(body, &ex)
if err != nil {
fmt.Println(err)
}
fmt.Println(ex.Topics)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论