0 like 0 dislike
23 views
asked in Programming by (5.6k points)

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

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

Your answer

Try to answer the question as detailed as possible.
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 (5.6k 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 6 views
0 like 0 dislike
1 answer 23 views
0 like 0 dislike
1 answer 13 views
0 like 0 dislike
1 answer 10 views
0 like 0 dislike
1 answer 44 views
0 like 0 dislike
1 answer 16 views
0 like 0 dislike
1 answer 20 views
0 like 0 dislike
1 answer 16 views
...