英文:
Error unmarshalling JSON in Go that starts with an Array
问题
这是我的代码
package main
import (
"encoding/json"
"log"
)
type Data struct {
Page int
Pages int
PerPage string
Total int
CountriesList []Country
}
type Country struct {
Id string
Iso string
}
func main() {
body := []byte(`[
{
"page": 1,
"pages": 6,
"per_page": "50",
"total": 256
},
[
{
"id": "ABW",
"iso2Code": "AW"}]]`)
items := make([]Data, 10)
if err := json.Unmarshal(body, &items); err != nil {
log.Fatalf("error %v", err)
}
}
我试图解析一些JSON并得到以下错误:
错误:无法将数组解析为类型为main.Data的Go值
英文:
This is my code
package main
import (
"encoding/json"
"log"
)
type Data struct {
Page int
Pages int
PerPage string
Total int
CountriesList []Country
}
type Country struct {
Id string
Iso string
}
func main() {
body := []byte(`[
{
"page": 1,
"pages": 6,
"per_page": "50",
"total": 256
},
[
{
"id": "ABW",
"iso2Code": "AW"}]]`)
items := make([]Data, 10)
if err := json.Unmarshal(body, &items); err != nil {
log.Fatalf("error %v", err)
}
}
I'm try to unmarshall some JSON and getting the following error:
error json: cannot unmarshal array into Go value of type main.Data
答案1
得分: 2
package main
import (
"encoding/json"
"fmt"
"log"
)
type Data struct {
Page int
Pages int
PerPage string json:"per_page, string"
Total int
}
type Country struct {
Id string
Iso2Code string
}
type DataCountry struct {
Data Data
CountryList []Country
}
func main() {
body := []byte([ { "page": 1, "pages": 6, "per_page": "50", "total": 256 }, [ { "id": "ABW", "iso2Code": "AW"}] ]
)
raw := make([]json.RawMessage, 10)
if err := json.Unmarshal(body, &raw); err != nil {
log.Fatalf("error %v", err)
}
sdc := make([]DataCountry, 0)
for i := 0; i < len(raw); i += 2 {
dc := DataCountry{}
data := Data{}
if err := json.Unmarshal(raw[i], &data); err != nil {
fmt.Println("error %v", err)
} else {
dc.Data = data
}
var countries []Country
if err := json.Unmarshal(raw[i+1], &countries); err != nil {
fmt.Println("error %v", err)
} else {
dc.CountryList = countries
}
sdc = append(sdc, dc)
}
fmt.Printf("%v\n", sdc)
}
英文:
When reading the question, I assumed that there could be multiple Data + list of Country pairs. Here's the solution I ended up with:
package main
import (
"encoding/json"
"fmt"
"log"
)
type Data struct {
Page int
Pages int
PerPage string `json:"per_page, string"`
Total int
}
type Country struct {
Id string
Iso2Code string
}
type DataCountry struct {
Data Data
CountryList []Country
}
func main() {
body := []byte(`[
{
"page": 1,
"pages": 6,
"per_page": "50",
"total": 256
},
[
{
"id": "ABW",
"iso2Code": "AW"}]
]`)
raw := make([]json.RawMessage, 10)
if err := json.Unmarshal(body, &raw); err != nil {
log.Fatalf("error %v", err)
}
sdc := make([]DataCountry, 0)
for i := 0; i < len(raw); i += 2 {
dc := DataCountry{}
data := Data{}
if err := json.Unmarshal(raw[i], &data); err != nil {
fmt.Println("error %v", err)
} else {
dc.Data = data
}
var countries []Country
if err := json.Unmarshal(raw[i+1], &countries); err != nil {
fmt.Println("error %v", err)
} else {
dc.CountryList = countries
}
sdc = append(sdc, dc)
}
fmt.Printf("%v\n", sdc)
}
I found the blog post "Using go to unmarshal json lists with multiple types" very useful for understanding several different options for dealing parsing JSON lists.
答案2
得分: 1
[]main.Data{main.Data{Page:1, Pages:6, Per_Page:"50", Total:256, CountriesList:[]main.Country{main.Country{Id:"ABW", Iso2Code:"AW"}}}}
英文:
package main
import (
"encoding/json"
"fmt"
"log"
)
type Data struct {
Page int
Pages int
Per_Page string
Total int
CountriesList []Country
}
type Country struct {
Id string
Iso2Code string
}
func main() {
body := []byte(`
[
{
"page": 1,
"pages": 6,
"per_page": "50",
"total": 256,
"countrieslist": [
{
"id": "ABW",
"iso2Code": "AW"
}
]
}
]
`)
items := make([]Data, 10)
if err := json.Unmarshal(body, &items); err != nil {
log.Fatalf("error %v", err)
}
fmt.Printf("%#v\n", items)
}
Output:
[]main.Data{main.Data{Page:1, Pages:6, Per_Page:"50", Total:256, CountriesList:[]main.Country{main.Country{Id:"ABW", Iso2Code:"AW"}}}}
答案3
得分: 1
package main
import (
"encoding/json"
"fmt"
"log"
)
type T1 struct {
Page, Pages, Total int
PerPage int json:"per_page,string"
}
type T2 struct {
ID string
ISO2Code string
}
func main() {
body := []byte([ { "page": 1, "pages": 6, "per_page": "50", "total": 256 }, [ { "id": "ABW", "iso2Code": "AW" }, { "id": "AFG", "iso2Code": "AF" } ] ]
)
t1 := T1{}
t2 := []T2{}
if err := json.Unmarshal(body, &[]interface{}{&t1, &t2}); err != nil {
log.Fatalf("error %v", err)
}
fmt.Printf("%#v %#v", t1, t2)
for k, v := range t2 {
fmt.Printf("%v %v\n", k, v.ID)
}
}
英文:
I got some help from #go-nuts on IRC:
package main
import (
"encoding/json"
"fmt"
"log"
)
type T1 struct {
Page, Pages, Total int
PerPage int `json:"per_page,string"`
}
type T2 struct {
ID string
ISO2Code string
}
func main() {
body := []byte(`
[
{
"page": 1,
"pages": 6,
"per_page": "50",
"total": 256
},
[
{
"id": "ABW",
"iso2Code": "AW"
},
{
"id": "AFG",
"iso2Code": "AF"
}
]
]
`)
t1 := T1{}
t2 := []T2{}
if err := json.Unmarshal(body, &[]interface{}{&t1, &t2}); err != nil {
log.Fatalf("error %v", err)
}
fmt.Printf("%#v %#v", t1, t2)
for k, v := range t2 {
fmt.Printf("%v %v\n",k, v.ID)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论