Re: [go-nuts] Struct confusion... what am I missing?
jake6502 is correct - the result is a slice that needs to be indexed (you might use jsonvals[2].Int()). I'd suggest that instead of using that gjson package, you might consider using the standard library's encoding/json package, which enables you to define a struct type that mimics the expected JSON; something like this: https://play.golang.org/p/uodaEwbjDZR This is nice because the json package takes care of a lot of validation before you need to look at the data. On 12 March 2018 at 15:42, wrote: > Go Noob here, confused about struct. My result is type []gjson.Result but I > can't extract a named value from this struct. Help greatly appreciated, I > have tried several articles. > > package main > > import ( >"fmt" >"github.com/tidwall/gjson" >"reflect" > ) > > > func main() { >// JSON package gjson >const json = `{"pass":true,"balance":180,"who":"f","ID":4231}]` > >jsonvals := gjson.GetMany(string(json), "pass", "balance", "ID") >fmt.Printf("%+v\n", jsonvals) // result: [true 180 4231] >fmt.Println(reflect.TypeOf(jsonvals)) // result: []gjson.Result >fmt.Printf("\n%+v\n", jsonvals.ID) // fails here, why? it is a struct, > right??? > > } > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [go-nuts] Struct confusion... what am I missing?
jake6502 is correct - the result is a slice that needs to be indexed (you might use jsonvals[2].Int()). I'd suggest that instead of using that gjson package, you might consider using the standard library's encoding/json package, which enables you to define a struct type that mimics the expected JSON; something like this: https://play.golang.org/p/uodaEwbjDZR This is nice because the json package takes care of a lot of validation before you need to look at the data. On 12 March 2018 at 15:42, wrote: > Go Noob here, confused about struct. My result is type []gjson.Result but I > can't extract a named value from this struct. Help greatly appreciated, I > have tried several articles. > > package main > > import ( >"fmt" >"github.com/tidwall/gjson" >"reflect" > ) > > > func main() { >// JSON package gjson >const json = `{"pass":true,"balance":180,"who":"f","ID":4231}]` > >jsonvals := gjson.GetMany(string(json), "pass", "balance", "ID") >fmt.Printf("%+v\n", jsonvals) // result: [true 180 4231] >fmt.Println(reflect.TypeOf(jsonvals)) // result: []gjson.Result >fmt.Printf("\n%+v\n", jsonvals.ID) // fails here, why? it is a struct, > right??? > > } > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] Struct confusion... what am I missing?
Go Noob here, confused about struct. My result is type []gjson.Result but I can't extract a named value from this struct. Help greatly appreciated, I have tried several articles. package main import ( "fmt" "github.com/tidwall/gjson" "reflect" ) func main() { // JSON package gjson const json = `{"pass":true,"balance":180,"who":"f","ID":4231}]` jsonvals := gjson.GetMany(string(json), "pass", "balance", "ID") fmt.Printf("%+v\n", jsonvals) // result: [true 180 4231] fmt.Println(reflect.TypeOf(jsonvals)) // result: []gjson.Result fmt.Printf("\n%+v\n", jsonvals.ID) // fails here, why? it is a struct, right??? } -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.