最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSON object using numbers - Stack Overflow

programmeradmin0浏览0评论

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 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... – user1106925 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. – user1106925 Commented Feb 21, 2012 at 16:20
Add a ment  | 

3 Answers 3

Reset to default 9

jsonObj["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/
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>