0 like 0 dislike
18 views
asked in Programming by (4.7k points)

How to generate JSON string with an integer as key?
json key as integer

{
	0: "foo",
	2: "bar",
	1: "str"
}

Your answer

pre, code

Preview

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

1 Answer

0 like 0 dislike
answered by (4.7k points)

In JSON can't use integer as key. JSON only allows key names to be strings. Those strings can consist of numerical values.

You can use identifiers for keys, but an identifier can't start with a number. You can still use strings though:

{
	"0": "foo",
	"2": "bar",
	"1": "str"
}

Related questions

0 like 0 dislike
1 answer 13 views
0 like 0 dislike
1 answer 5 views
0 like 0 dislike
1 answer 8 views
0 like 0 dislike
1 answer 36 views
0 like 0 dislike
1 answer 8 views
0 like 0 dislike
1 answer 10 views
0 like 0 dislike
1 answer 11 views
0 like 0 dislike
1 answer 13 views
...