如何在Golang中反序列化具有匿名数组的JSON?

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

How to deserialize a json with an array of anonymous arrays in Golang?

问题

我正在从外部服务器接收到这个 JSON 数据:

[["010117", "070117", "080117"], ["080117", "140117", "150117"], ["150117", "210117", "220117"]]

我需要解析它。

package main

import (
	"encoding/json"
	"fmt"
	"io"
	"os"
	"runtime"
)

type Range struct {
	From string
	To   string
	Do   string
}

type AllRanges struct {
	Ranges []Range
}

func main() {
	var ranges AllRanges
	j, err := os.ReadFile(file)

	if err != nil {
		panic("无法读取 JSON 文件")
	}

	if json.Unmarshal(j, &v) != nil {
		panic("读取 JSON 时出错")
	}
}

当我执行时,会抛出一个 panic,指示读取 JSON 时出错。

提前感谢!

英文:

I am receiving this json from an external server:

[["010117", "070117", "080117"], ["080117", "140117", "150117"], ["150117", "210117", "220117"]]

and i need to parse it

package main

import (
	"encoding/json"
	"fmt"
	"io"
	"os"
	"runtime"
)

type Range struct {
	From string
	To   string
	Do   string
}

type AllRanges struct {
	Ranges []Range
}

func main() {
	var ranges AllRanges
	j, err := os.ReadFile(file)

	if err != nil {
		panic("Can't read json file")
	}

	if json.Unmarshal(j, &v) != nil {
		panic("Error reading the json")
	}
}

When I execute, a panic it is thrown indicating an error reading the json

Thanks in advance !

答案1

得分: 2

  1. 这不是出错的代码。你发布的代码无法编译,因为它试图将数据解组成一个未声明的变量 v

  2. 假设 v 应该是 ranges,问题非常简单...

ranges 是类型为 AllRanges 的结构体,它有一个名为 Ranges 的成员,该成员是一个结构体数组,每个结构体也有命名的成员。

因此,当尝试将 JSON 解组成这个结构体时,解组器将期望找到:

{
   "Ranges": [
        { 
           "From": "..",
           "To": ..,
           "Do": ".."
        },
        { etc }
   ]
}

要解组你的数据,它由一个匿名的字符串数组的数组组成,你需要将 ranges 声明为一个字符串数组的数组:

    var ranges [][]string

    ...

    if json.Unmarshal(j, &ranges) != nil {
        panic("读取 JSON 错误")
    }

一旦你解组成这个数组的数组,你就需要编写代码将其转换为所需的结构化值。

这个 playground 成功地将你的示例数据解组成了 [][]string。转换留作练习。

英文:
  1. This isn't the code that is failing. The code you have posted won't compile as it attempts to unmarshal into an undeclared variable, v.

  2. Assuming that v is supposed to be ranges, the problem is very simple....

ranges is of type AllRanges which is a struct having a named member Ranges which is an array of structs, also having named members.

Therefore, when attempting to unmarshal json into this struct, the unmarshaller will expect to find:

{
   "Ranges": [
        { 
           "From": "..",
           "To": ..,
           "Do": ".."
        },
        { etc }
   ]
}

To unmarshal your data, consisting of an anonymous array of arrays of string, you need instead to declare ranges as an array of array of strings:

    var ranges [][]string

    ...

    if json.Unmarshal(j, &ranges) != nil {
        panic("Error reading the json")
    }

Once you have unmarshalled into this array of arrays you will then need to write code to transform it into the desired structured values.

This playground demonstrates successfully unmarshalling your sample data into a [][]string. Transformation is left as an exercise.

huangapple
  • 本文由 发表于 2023年2月12日 01:54:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75422165.html
匿名

发表评论

匿名网友

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

确定