3

I am running into an issue when using json that I am not sure how to approach it. I create an object like this:

var jsonObj = {"000000": 0, "010000": 1, "020000": 0 .... };

where the 0's and 1's are to act as bits. However if I were to try and call this object:

alert(jsonObj.000000);

I get an "Unexpected number" error in Chrome because it is handling the 00000 as a number and not a string. If I were to restructure the json object with a letter before the 6 numbers "c000000" then

alert(jsonObj.c000000);

Would return a correct value. Curious if anyone else has experienced anything like this and how to handle it??

Matt
  • 1,265
  • 1
  • 16
  • 24
  • This question should be helpful - http://stackoverflow.com/questions/2940424/valid-javascript-object-property-names – maerics Feb 21 '12 at 01:54
  • FYI: That isn't JSON. It's just a typical JavaScript object created using object literal notation. –  Feb 21 '12 at 02:49
  • @amnotiam I was under the impression that JSON could be done two ways - 1 as an object like I did above using { pair, pair, pair, ... } and 2 as an array. Yeah it is an object but that's how json (javascript object notation) can be used. – Matt Feb 21 '12 at 15:59
  • @Matt: JSON is a text format for transferring data between different programming environments. The `JS` in `JSON` does stand for JavaScript, but that's just because the syntax for its data structures were *based on* JavaScript's object and array literal notations. This is a point of confusion for many people. When you're writing JavaScript code, and you do `var foo = {"bar":"baz"}`, you haven't technically created any JSON data even though the same object structure could be used to create JSON. Since JSON is text, within the JavaScript environment, valid JSON would be represented with... –  Feb 21 '12 at 16:18
  • ...a `String`, for example `var myJSON = '{"bar":"baz"}'`, although you wouldn't often see this, since JSON text is typically created as markup on a server. But if that JSON markup is fetched from a server, say via an AJAX request, it will show up as text converted into a `String` object, which would then need to be parsed into an actual JavaScript object that can be manipulated. –  Feb 21 '12 at 16:20

3 Answers3

9

jsonObj["000000"] should work

Thilo
  • 257,207
  • 101
  • 511
  • 656
1

You must do:

alert(jsonObj["000000"]);

See this question for variable name rules.

Community
  • 1
  • 1
YXD
  • 31,741
  • 15
  • 75
  • 115
0

Try:

alert(jsonObj["000000"])

Here's a jsFiddle of the above:

icyrock.com
  • 27,952
  • 4
  • 66
  • 85