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??
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??
Share Improve this question asked Feb 21, 2012 at 1:50 MattMatt 1,2651 gold badge16 silver badges24 bronze badges 5- This question should be helpful - stackoverflow./questions/2940424/… – maerics Commented Feb 21, 2012 at 1:54
- FYI: That isn't JSON. It's just a typical JavaScript object created using object literal notation. – user1106925 Commented Feb 21, 2012 at 2: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 Commented Feb 21, 2012 at 15:59
-
@Matt: JSON is a text format for transferring data between different programming environments. The
JS
inJSON
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 dovar 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... – user1106925 Commented Feb 21, 2012 at 16:18 -
...a
String
, for examplevar 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 aString
object, which would then need to be parsed into an actual JavaScript object that can be manipulated. – user1106925 Commented Feb 21, 2012 at 16:20
3 Answers
Reset to default 9jsonObj["000000"]
should work
You must do:
alert(jsonObj["000000"]);
See this question for variable name rules.
Try:
alert(jsonObj["000000"])
Here's a jsFiddle of the above:
- http://jsfiddle/hzrf7/