解析JSON的问题

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

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)

playground

英文:

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)

<kbd>playground</kbd>

huangapple
  • 本文由 发表于 2014年7月11日 05:07:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/24686293.html
匿名

发表评论

匿名网友

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

确定