Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to convert gjson object to struct #193

Closed
mf0324 opened this issue Dec 10, 2020 · 1 comment
Closed

how to convert gjson object to struct #193

mf0324 opened this issue Dec 10, 2020 · 1 comment

Comments

@mf0324
Copy link

mf0324 commented Dec 10, 2020

I have a question, I have a func have gjson object parameter,for example:

type mapJSONTest struct {
Test map[int]int
}

func convertStruct(obj *gjson.Json) {
// how can convert obj to mapJSONTest
// of course I can
//var gobj mapJSONTest
// body, _ := obj.ToJson()
//gjson.DecodeTo(body, &gobj)
}

func main() {
mapTest := make(map[int]int)
mapTest[1000] = 2
mapTest[2000] = 4
testObj := mapJSONTest{}
testObj.Test = mapTest

    body, _ := gjson.Encode(testObj)

    jsonObj, _ := gjson.DecodeToJson(body)

    convertStruct(jsonObj)

}

@tidwall
Copy link
Owner

tidwall commented Dec 10, 2020

There no endode/decode functions with gjson. I recommend using the standard json package for that kind of functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants