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??

5
  • This question should be helpful - stackoverflow.com/questions/2940424/… Commented Feb 21, 2012 at 1:54
  • FYI: That isn't JSON. It's just a typical JavaScript object created using object literal notation. 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. Commented Feb 21, 2012 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... Commented Feb 21, 2012 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. Commented Feb 21, 2012 at 16:20

3 Answers 3

9

jsonObj["000000"] should work

Sign up to request clarification or add additional context in comments.

1 Comment

that works perfectly... I knew it was something simple... thanks!
1

You must do:

alert(jsonObj["000000"]);

See this question for variable name rules.

Comments

0

Try:

alert(jsonObj["000000"])

Here's a jsFiddle of the above:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.