How do I iterate over a JSON array in Golang?

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

How do I iterate over a JSON array in Golang?

问题

我正在尝试解码一个JSON数组并将其放入一个结构体切片中。我已经了解了如何做到这一点,但前提是JSON数组包含键。而我的JSON数组没有包含键。

我已经将程序简化为仅处理JSON数据的部分。它可以编译,并且可以在下面找到。

package main

// 2014-04-19

import (
    "fmt"
    "encoding/json"
)

type itemdata struct {
    data1 int // 我尝试将这些字段改为字符串类型
    data2 int
    data3 int
}

func main() {
    datas := []itemdata{}
        
    json.Unmarshal([]byte(`[["7293","1434","99646"],["4657","1051","23795"]]`), &datas)
    // 我尝试过不在数字周围加引号的JSON字符串
    fmt.Println(len(datas)) // 这将打印'2'
    fmt.Println("This prints") // 这将打印
    for i := range datas {
        fmt.Println(datas[i].data1)  // 这将打印'0',两次
    }
    fmt.Println("And so does this") // 这将打印
}

我搜索了类似"Go语言无键JSON解码"的内容,并阅读了Go语言网站上的文章和包页面。我可以找到足够的关于如何处理Go和JSON的信息,但是我找到的文章都没有解释如何在JSON数组中没有键的情况下处理它。

如果我得到一个错误,我不会感到奇怪;JSON的值是字符串形式的数字(这是我输入的方式),但我试图将它们放入整数中。然而,我没有得到错误。我尝试将itemdata结构体中的值改为字符串,但效果不大。删除JSON值中的引号也没有帮助。

我想知道如何将我的JSON数组转换为itemdata的切片。前三个值将分别放入itemdata.data1itemdata.data2itemdata.data3中。

如果您认为我可以改进我的问题,请告诉我。

提前感谢,
Remi

英文:

I am trying to decode a JSON array and put it in a slice of a struct. I've read how to do this, but only if the JSON array contains keys. My JSON array does not contain keys.

I have stripped the program down to only the part where it handles the JSON data. It compiles and can be found below.

package main

// 2014-04-19

import (
    "fmt"
    "encoding/json"
)

type itemdata struct {
    data1 int // I have tried making these strings
    data2 int
    data3 int
}

func main() {
    datas := []itemdata{}
        
    json.Unmarshal([]byte(`[["7293","1434","99646"],["4657","1051","23795"]]`), &datas)
    // I have tried the JSON string without the qoutes around the numbers
    fmt.Println(len(datas)) // This prints '2'
    fmt.Println("This prints") // This does print 
    for i := range datas {
        fmt.Println(datas[i].data1)  // This prints '0', two times 
    }
    fmt.Println("And so does this") // This does print
}

I've searched for things like 'Go Lang JSON decode without keys' and read articles (and 'package pages') on the Go Lang website. I can find enough information on how to work with Go and JSON, but none of my found articles explain how to do it without keys in the JSON array.

I wouldn't find it odd if I would get an error; The JSON values are stringy-numbers (that's how I get them as input), but I am trying to put them in integers. I am not getting an error though. I have tried making the values in the 'itemdata' struct strings, that didn't help much. Removing the quotes from the JSON values didn't help either.

I would like to know how I can make my JSON array in a slice of 'itemdata'. The first out of three values would go into 'itemdata.data1', the second in 'itemdata.data2' and the third in 'itemdata.data3'.

Please let me know if you think I can improve my question.

Thanks in advance,
Remi

答案1

得分: 9

你这里有一个二维字符串数组。你可以像这样解码它:

type itemdata [][]string

func main() {
   var datas itemdata

    json.Unmarshal([]byte(`[["7293","1434","99646"],["4657","1051","23795"]]`), &datas)
    fmt.Println(len(datas))
    fmt.Println("This prints")
    for i := range datas {
        fmt.Println(datas[i][1]) 
    }
    fmt.Println("And so does this")
}

演示

英文:

What you have here is a bi-dimensional array of strings. You can decode it like this :

type itemdata [][]string

func main() {
   var datas itemdata

    json.Unmarshal([]byte(`[["7293","1434","99646"],["4657","1051","23795"]]`), &datas)
    fmt.Println(len(datas))
    fmt.Println("This prints")
    for i := range datas {
        fmt.Println(datas[i][1]) 
    }
    fmt.Println("And so does this")
}

Demonstration

huangapple
  • 本文由 发表于 2014年4月25日 15:46:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/23287140.html
匿名

发表评论

匿名网友

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

确定