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

jquery - Javascript: access an object property whose name starts with a number - Stack Overflow

programmeradmin2浏览0评论

I'm creating a Javascript / jQuery application.

I need to process a JSON response that represents a HashMap, like this:

  {
    "accounts": {
      "MediaFire": {
       "provider": "MediaFire",
       "usedStorage": "779680",
       "totalStorage": "53687091200"
      },
      "4Sync": {
       "provider": "4Sync",
       "usedStorage": "620692",
       "totalStorage": "16106127360"
      }
    }
  }

I use a pasing function (which I can't control), which returns the parsed JSON response in an object result.

When I try to access the 4Sync like this:

var usedStorage = result.accounts.4Sync.usedStorage; //doesn't work

it doesn't work, I think it's because of the 4 at the beginning... The same operation with the other object works fine:

var usedStorage = result.accounts.MediaFire.usedStorage; //works

I know the result object contains the object 4Sync, but I can't access it. Here is a screenshot of Chrome's console:

Is there any workaround to solve this?

I'm creating a Javascript / jQuery application.

I need to process a JSON response that represents a HashMap, like this:

  {
    "accounts": {
      "MediaFire": {
       "provider": "MediaFire",
       "usedStorage": "779680",
       "totalStorage": "53687091200"
      },
      "4Sync": {
       "provider": "4Sync",
       "usedStorage": "620692",
       "totalStorage": "16106127360"
      }
    }
  }

I use a pasing function (which I can't control), which returns the parsed JSON response in an object result.

When I try to access the 4Sync like this:

var usedStorage = result.accounts.4Sync.usedStorage; //doesn't work

it doesn't work, I think it's because of the 4 at the beginning... The same operation with the other object works fine:

var usedStorage = result.accounts.MediaFire.usedStorage; //works

I know the result object contains the object 4Sync, but I can't access it. Here is a screenshot of Chrome's console:

Is there any workaround to solve this?

Share Improve this question edited Apr 18, 2013 at 15:48 MikO asked Apr 18, 2013 at 13:09 MikOMikO 18.8k13 gold badges80 silver badges112 bronze badges 2
  • Note that this issue has nothing to do with JSON: JSON is the string representation before you parse it. Your result variable references an object that is the result of parsing the JSON. The issue is "how to access an object property that starts with a digit"... – nnnnnn Commented Apr 18, 2013 at 13:25
  • @nnnnnn That's definitely true, I've edited the title... – MikO Commented Apr 18, 2013 at 15:03
Add a ment  | 

1 Answer 1

Reset to default 9

Use square brackets:

var usedStorage = result.accounts["4Sync"].usedStorage;

Property identifers can begin with a number, but member expressions with the . character will only allow valid variable identifiers (since anything else is ambiguous). To get around this, you can use the square bracket syntax, which is equivalent but allows the use of any string.

If you're interested, here is the grammar:

MemberExpression :
    PrimaryExpression
    FunctionExpression
    MemberExpression [ Expression ]
    MemberExpression . IdentifierName

Notice how square brackets can contain any expression, but the . can only be followed by an IdentifierName (basically, any valid identifier, plus reserved words in ES5).

发布评论

评论列表(0)

  1. 暂无评论