How can I count the number of properties in a struct or JSON document?
up vote
1
down vote
favorite
I'm making a PDF generator in Go and one of the sections of it will be a table. To create a table I need to state the width of the columns, and this will be done by getting the page width (minus margins) and dividing by the number of columns in the table
The columns in the table are defined in a struct like this:
type Person struct {
Name string `json:"Name"`
Age string `json:"Age"`
Comment string `json:"Comment"`
}
And JSON is unmarshalled into it
I don't want to have to hardcode '3' as the column number into my code and want to know how I can programmatically count the properties either in from the JSON or the struct itself
I've spent a few days searching now, and all results focus on people having trouble getting the values, but I want the keys!
Thanks in advance
json
add a comment |
up vote
1
down vote
favorite
I'm making a PDF generator in Go and one of the sections of it will be a table. To create a table I need to state the width of the columns, and this will be done by getting the page width (minus margins) and dividing by the number of columns in the table
The columns in the table are defined in a struct like this:
type Person struct {
Name string `json:"Name"`
Age string `json:"Age"`
Comment string `json:"Comment"`
}
And JSON is unmarshalled into it
I don't want to have to hardcode '3' as the column number into my code and want to know how I can programmatically count the properties either in from the JSON or the struct itself
I've spent a few days searching now, and all results focus on people having trouble getting the values, but I want the keys!
Thanks in advance
json
there is no way other than usingreflectpackage, but I strongly suggest change your approach
– danicheeta
Nov 19 at 11:01
Can I ask why? The PDF package is Gofpdf, to generate a table it basically 'draws' individual cells and then stitches them together, meaning you have to get the individual cell width
– MassiveOwl
Nov 19 at 11:06
1
@danicheeta: Why? Reflection is necessary for this problem anyway.
– Flimzy
Nov 19 at 11:17
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm making a PDF generator in Go and one of the sections of it will be a table. To create a table I need to state the width of the columns, and this will be done by getting the page width (minus margins) and dividing by the number of columns in the table
The columns in the table are defined in a struct like this:
type Person struct {
Name string `json:"Name"`
Age string `json:"Age"`
Comment string `json:"Comment"`
}
And JSON is unmarshalled into it
I don't want to have to hardcode '3' as the column number into my code and want to know how I can programmatically count the properties either in from the JSON or the struct itself
I've spent a few days searching now, and all results focus on people having trouble getting the values, but I want the keys!
Thanks in advance
json
I'm making a PDF generator in Go and one of the sections of it will be a table. To create a table I need to state the width of the columns, and this will be done by getting the page width (minus margins) and dividing by the number of columns in the table
The columns in the table are defined in a struct like this:
type Person struct {
Name string `json:"Name"`
Age string `json:"Age"`
Comment string `json:"Comment"`
}
And JSON is unmarshalled into it
I don't want to have to hardcode '3' as the column number into my code and want to know how I can programmatically count the properties either in from the JSON or the struct itself
I've spent a few days searching now, and all results focus on people having trouble getting the values, but I want the keys!
Thanks in advance
json
json
asked Nov 19 at 10:59
MassiveOwl
468313
468313
there is no way other than usingreflectpackage, but I strongly suggest change your approach
– danicheeta
Nov 19 at 11:01
Can I ask why? The PDF package is Gofpdf, to generate a table it basically 'draws' individual cells and then stitches them together, meaning you have to get the individual cell width
– MassiveOwl
Nov 19 at 11:06
1
@danicheeta: Why? Reflection is necessary for this problem anyway.
– Flimzy
Nov 19 at 11:17
add a comment |
there is no way other than usingreflectpackage, but I strongly suggest change your approach
– danicheeta
Nov 19 at 11:01
Can I ask why? The PDF package is Gofpdf, to generate a table it basically 'draws' individual cells and then stitches them together, meaning you have to get the individual cell width
– MassiveOwl
Nov 19 at 11:06
1
@danicheeta: Why? Reflection is necessary for this problem anyway.
– Flimzy
Nov 19 at 11:17
there is no way other than using
reflect package, but I strongly suggest change your approach– danicheeta
Nov 19 at 11:01
there is no way other than using
reflect package, but I strongly suggest change your approach– danicheeta
Nov 19 at 11:01
Can I ask why? The PDF package is Gofpdf, to generate a table it basically 'draws' individual cells and then stitches them together, meaning you have to get the individual cell width
– MassiveOwl
Nov 19 at 11:06
Can I ask why? The PDF package is Gofpdf, to generate a table it basically 'draws' individual cells and then stitches them together, meaning you have to get the individual cell width
– MassiveOwl
Nov 19 at 11:06
1
1
@danicheeta: Why? Reflection is necessary for this problem anyway.
– Flimzy
Nov 19 at 11:17
@danicheeta: Why? Reflection is necessary for this problem anyway.
– Flimzy
Nov 19 at 11:17
add a comment |
2 Answers
2
active
oldest
votes
up vote
5
down vote
accepted
reflect.TypeOf(Person{}).NumField()
or
len(structs.Map(Person{}))
(you need to import "github.com/fatih/structs")
Nice, thanks for this - works great
– MassiveOwl
Nov 19 at 11:14
add a comment |
up vote
-1
down vote
The Object.getOwnPropertyNames(object) will return an array of property name for the given object
Thanks, i'll try this
– MassiveOwl
Nov 19 at 11:06
2
This question is about Go.
– Flimzy
Nov 19 at 11:17
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
reflect.TypeOf(Person{}).NumField()
or
len(structs.Map(Person{}))
(you need to import "github.com/fatih/structs")
Nice, thanks for this - works great
– MassiveOwl
Nov 19 at 11:14
add a comment |
up vote
5
down vote
accepted
reflect.TypeOf(Person{}).NumField()
or
len(structs.Map(Person{}))
(you need to import "github.com/fatih/structs")
Nice, thanks for this - works great
– MassiveOwl
Nov 19 at 11:14
add a comment |
up vote
5
down vote
accepted
up vote
5
down vote
accepted
reflect.TypeOf(Person{}).NumField()
or
len(structs.Map(Person{}))
(you need to import "github.com/fatih/structs")
reflect.TypeOf(Person{}).NumField()
or
len(structs.Map(Person{}))
(you need to import "github.com/fatih/structs")
edited Nov 19 at 11:23
answered Nov 19 at 11:12
Ehsan.Saradar
41827
41827
Nice, thanks for this - works great
– MassiveOwl
Nov 19 at 11:14
add a comment |
Nice, thanks for this - works great
– MassiveOwl
Nov 19 at 11:14
Nice, thanks for this - works great
– MassiveOwl
Nov 19 at 11:14
Nice, thanks for this - works great
– MassiveOwl
Nov 19 at 11:14
add a comment |
up vote
-1
down vote
The Object.getOwnPropertyNames(object) will return an array of property name for the given object
Thanks, i'll try this
– MassiveOwl
Nov 19 at 11:06
2
This question is about Go.
– Flimzy
Nov 19 at 11:17
add a comment |
up vote
-1
down vote
The Object.getOwnPropertyNames(object) will return an array of property name for the given object
Thanks, i'll try this
– MassiveOwl
Nov 19 at 11:06
2
This question is about Go.
– Flimzy
Nov 19 at 11:17
add a comment |
up vote
-1
down vote
up vote
-1
down vote
The Object.getOwnPropertyNames(object) will return an array of property name for the given object
The Object.getOwnPropertyNames(object) will return an array of property name for the given object
edited Nov 19 at 14:14
AmerllicA
2,90211033
2,90211033
answered Nov 19 at 11:04
Shiju
1533
1533
Thanks, i'll try this
– MassiveOwl
Nov 19 at 11:06
2
This question is about Go.
– Flimzy
Nov 19 at 11:17
add a comment |
Thanks, i'll try this
– MassiveOwl
Nov 19 at 11:06
2
This question is about Go.
– Flimzy
Nov 19 at 11:17
Thanks, i'll try this
– MassiveOwl
Nov 19 at 11:06
Thanks, i'll try this
– MassiveOwl
Nov 19 at 11:06
2
2
This question is about Go.
– Flimzy
Nov 19 at 11:17
This question is about Go.
– Flimzy
Nov 19 at 11:17
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373171%2fhow-can-i-count-the-number-of-properties-in-a-struct-or-json-document%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
there is no way other than using
reflectpackage, but I strongly suggest change your approach– danicheeta
Nov 19 at 11:01
Can I ask why? The PDF package is Gofpdf, to generate a table it basically 'draws' individual cells and then stitches them together, meaning you have to get the individual cell width
– MassiveOwl
Nov 19 at 11:06
1
@danicheeta: Why? Reflection is necessary for this problem anyway.
– Flimzy
Nov 19 at 11:17