如何将字符串转换为结构体数组

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

How to convert string to struct array

问题

我有一个转换为字符串的 JSON 数组。现在我想将该字符串映射到一个结构体数组中,以便我可以修改字符串中的 JSON。以下是我的代码:

type ProcessdetailsEntity struct {
	Source   []int64 `json:"source"`
	Node     string  `json:"node"`
	Targets  []int64 `json:"targets"`
	Issave   bool    `json:"isSave"`
	Instate  []int64 `json:"inState"`
	OutState []int64 `json:"outState"`
}

func main() {
	var stringJson = `[{"source":[-1],"node":"1_1628008588902","targets":[],"isSave":true,"inState":[1],"outState":[2]},{"source":["1_1628008588902","5_1628008613446"],"node":"2_1628008595757","targets":[],"isSave":true,"inState":[2,5],"outState":[3,6]}]`
	in := []byte(stringJson)
	detailsEntity := []ProcessdetailsEntity{}
	err := json.Unmarshal(in, &detailsEntity)
	if err != nil {
		log.Print(err)
	}
}

当我运行这段代码时,我得到了以下错误:

json: cannot unmarshal string into Go struct field ProcessdetailsEntity.source of type int64

如何正确地将字符串映射到结构体,以便我可以修改 JSON 中的 inStateoutState 值?

英文:

I have a json array which is converted into a string. Now I want to map the string to a struct array so that I can modify the string json. Below is my code base

type ProcessdetailsEntity struct {
	Source   []int64 `json:"source"`
	Node     string  `json:"node"`
	Targets  []int64 `json:"targets"`
	Issave   bool    `json:"isSave"`
	Instate  []int64 `json:"inState"`
	OutState []int64 `json:"outState"`
}

func main() {
	var stringJson = "[{\"source\":[-1],\"node\":\"1_1628008588902\",\"targets\":[],\"isSave\":true,\"inState\":[1],\"outState\":[2]},{\"source\":[\"1_1628008588902\",\"5_1628008613446\"],\"node\":\"2_1628008595757\",\"targets\":[],\"isSave\":true,\"inState\":[2,5],\"outState\":[3,6]}]"
	in := []byte(stringJson)
	detailsEntity := []ProcessdetailsEntity{}
	err := json.Unmarshal(in, &detailsEntity)
	if err != nil {
		log.Print(err)
	}
}

Now when I run this code base I got the error:

json: cannot unmarshal string into Go struct field ProcessdetailsEntity.source of type int64

How to properly map the string to struct so that I can modify the inState and outState value of the json ?

答案1

得分: 2

你得到的错误已经非常明确了:

> 无法将字符串解组为类型为 int64 的 ProcessdetailsEntity.source 字段

这告诉你,你的 source 字段中至少有一个的类型似乎是错误的:一个 string 而不是可以用 int64 表示的类型。

所以让我们检查一下你的 stringJson 中的 source 字段:

"source":[-1]
"source":["1_1628008588902","5_1628008613446"]

你可以看到第二个 source 是一个 string 数组。因此出现了错误。

要解决这个问题,你需要确保 source 是一个 int 数组。不幸的是,在 Go 中,1_16280085889025_1628008613446 不是有效的整数。

我稍微修改了你的 JSON 并修复了你的代码,然后它就可以工作了:

package main

import (
	"encoding/json"
	"log"
)

type ProcessdetailsEntity struct {
	Source   []int64 `json:"source"`
	Node     string  `json:"node"`
	Targets  []int64 `json:"targets"`
	Issave   bool    `json:"isSave"`
	Instate  []int64 `json:"inState"`
	OutState []int64 `json:"outState"`
}

func main() {
	var stringJson = `[
		{
			"source":[-1],
			"node":"1_1628008588902",
			"targets":[],
			"isSave":true,
			"inState":[1],
			"outState":[2]
		},
		{
			"source":[11628008588902,51628008613446],
			"node":"2_1628008595757",
			"targets":[],
			"isSave":true,
			"inState":[2,5],
			"outState":[3,6]
		}
	]`
	
	in := []byte(stringJson)
	detailsEntity := []ProcessdetailsEntity{}
	err := json.Unmarshal(in, &detailsEntity)
	if err != nil {
		log.Print(err)
	}
}

参考:https://play.golang.org/p/kcrkfRliWJ5

英文:

The error you get is already pretty much on the nose:

> cannot unmarshal string into Go struct field ProcessdetailsEntity.source of type int64

That tells you that (at least one) of your source fields appears to have the wrong type: a string instead of something that can be represented by a int64.

So let's check your source fields in your stringJson:

"source":[-1]
"source":["1_1628008588902","5_1628008613446"]

As you can see the second source is an array of string. Hence the error.

To solve this you need to make sure that the source is an array of int. Unfortunately, 1_1628008588902 and 5_1628008613446 are not valid integers in Go.

I slightly modified your JSON and fixed your code an then it works:

package main

import (
	"encoding/json"
	"log"
)

type ProcessdetailsEntity struct {
	Source   []int64 `json:"source"`
	Node     string  `json:"node"`
	Targets  []int64 `json:"targets"`
	Issave   bool    `json:"isSave"`
	Instate  []int64 `json:"inState"`
	OutState []int64 `json:"outState"`
}

func main() {
	var stringJson = `[
		{
			"source":[-1],
			"node":"1_1628008588902",
			"targets":[],
			"isSave":true,
			"inState":[1],
			"outState":[2]
		},
		{
			"source":[11628008588902,51628008613446],
			"node":"2_1628008595757",
			"targets":[],
			"isSave":true,
			"inState":[2,5],
			"outState":[3,6]
		}
	]`
	
	in := []byte(stringJson)
	detailsEntity := []ProcessdetailsEntity{}
	err := json.Unmarshal(in, &detailsEntity)
	if err != nil {
		log.Print(err)
	}
}

See: https://play.golang.org/p/kcrkfRliWJ5

huangapple
  • 本文由 发表于 2021年8月8日 15:43:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/68698743.html
匿名

发表评论

匿名网友

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

确定