将数组解组为Go结构体的easyjson方法

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

easyjson unmarshal array into go struct

问题

我最近开始使用golang和easyjson。现在我需要将一个json数组解组成一个结构体以便进行操作。

以下是我得到的内容。

传入的JSON数据如下所示:

[
{
"Item1": true,
"Item2": "hello",
"Item3": {
"A": 1
}
},
...
]

我的结构体如下:

package something

//easyjson:json
type Item struct {
Item1 bool
Item2 string
Item3 SubItem
}

//easyjson:json
type SubItem struct {
A int
}
(我生成了*_easyjson.go文件)

以下是我如何使用easyjson:

func ConvertJsonToItem(jsondata string) Item {
var item Item
lexer := jlexer.Lexer{Data: []byte(jsondata)}

item.UnmarshalEasyJSON(&lexer)
if lexer.Error() != nil {
    panic(lexer.Error())
}
return Item

}

我知道这样做行不通,因为json由一个"Item"结构体的数组组成。但是像这样写

var items []Item

也行不通,因为我不能在切片上执行UnmarshalEasyJSON操作。我使用easyjson是因为它是处理json数据的最快速的方式。

所以我的问题是:我如何使用easyjson处理对象数组。

顺便说一句,我知道这个问题与这个问题类似Unmarshal json into struct: cannot unmarshal array into Go value,但是那个用户使用的是内置的json包,而我有/想使用easyjson。
提前谢谢。

英文:

I started working with golang and easyjson recently. Now I have to unmarshal a json array into a struct to work with it.
So here's what I got.

The incoming JSON-data looks like this

[
    {
        "Item1":true,
        "Item2":"hello",
        "Item3":{
            "A": 1,
        },
    },
    ...
]

My struct:

package something

//easyjson:json
type Item struct {
    Item1 bool
    Item2 string
    Item3 SubItem
}

//easyjson:json
type SubItem struct {
    A int	   
}

(I build the *_easyjson.go file)

And here's how I would use easyjson:

func ConvertJsonToItem(jsondata string) Item {
	var item Item
	lexer := jlexer.Lexer{Data: []byte(jsondata)}

	item.UnmarshalEasyJSON(&lexer)
	if lexer.Error() != nil {
		panic(lexer.Error())
	}
	return Item
}

That won't work, I know, because the json consists of an array of "Item"-structs. But writing something like

var items []Item

won't work either because I can't execute UnmarshalEasyJSON on a slice.
I'm using easyjson because it's the fastet way to handle json data.

So my question is: how can I handle the object array with easyjson.

By the way, I know this question is similar to this one Unmarshal json into struct: cannot unmarshal array into Go value but there the user uses the builtin json package and I have/want to use easyjson.
Thank you in advance.

答案1

得分: 1

关于

//easyjson:json
type ItemSlice []Item

你可以这样做

func ConvertJsonToItem(jsondata string) ItemSlice {
    var items ItemSlice
    lexer := jlexer.Lexer{Data: []byte(jsondata)}

    items.UnmarshalEasyJSON(&lexer)
    if lexer.Error() != nil {
        panic(lexer.Error())
    }
    return items
}

如果你真的不想在外部代码中使用ItemSlice,你仍然可以在返回之前将其转换回[]Item(但是如果你以后想要进行编组操作,你将需要以相同的方式进行逆操作)。

英文:

What about

//easyjson:json
type ItemSlice []Item

?

Then, you can do

func ConvertJsonToItem(jsondata string) ItemSlice {
    var items ItemSlice
    lexer := jlexer.Lexer{Data: []byte(jsondata)}

    items.UnmarshalEasyJSON(&lexer)
    if lexer.Error() != nil {
        panic(lexer.Error())
    }
    return items
}

And if you really don't want an ItemSlice in your outer code, you can still convert it back to []Item before returning it (but you'll have to do the exact same operation in the other way it you want to marshal it later…).

huangapple
  • 本文由 发表于 2017年1月13日 18:53:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/41632922.html
匿名

发表评论

匿名网友

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

确定