如何在值的名称中使用连字符获取JSON值(javascript

时间:2016-08-13 04:26:24

标签: javascript json

所以我从网站上检索了以下JSON,我想知道如何从中检索一些数据。这是JSON数据的样子。

{ name: '★ StatTrak™ Bayonet | Tiger Tooth (Factory New)',
  price: 23763,
  history:
      { '2016-06-14': { price: 31967, count: 0 },
        '2016-06-15': { price: 33000, count: 0 },
        '2016-06-16': { price: 32005, count: 0 },
        '2016-06-17': { price: 30429, count: 0 },
        '2016-06-18': { price: 27797, count: 0 },
        '2016-06-19': { price: 28773, count: 0 },
        '2016-06-20': { price: 29653, count: 0 },
        '2016-06-21': { price: 30000, count: 0 },
        '2016-06-22': { price: 31900, count: 0 },
        '2016-06-23': { price: 27200, count: 0 },
        '2016-06-24': { price: 29350, count: 0 },
        '2016-06-26': { price: 33450, count: 0 },
        '2016-06-27': { price: 29433, count: 0 },
        '2016-06-28': { price: 28800, count: 0 },
        '2016-06-29': { price: 31000, count: 0 },
        '2016-06-30': { price: 30947, count: 0 },
        '2016-07-01': { price: 30150, count: 0 },
        '2016-07-03': { price: 31600, count: 0 },
        '2016-07-04': { price: 24800, count: 0 },
        '2016-07-05': { price: 34421, count: 0 },
        '2016-07-06': { price: 29958, count: 0 },
        '2016-07-07': { price: 29000, count: 0 },
        '2016-07-08': { price: 29757, count: 0 },
        '2016-07-10': { price: 30000, count: 0 },
        '2016-07-11': { price: 34100, count: 0 },
        '2016-07-12': { price: 33333, count: 0 },
        '2016-07-13': { price: 26750, count: 0 },
        '2016-07-14': { price: 25950, count: 0 },
        '2016-07-15': { price: 26933, count: 0 },
        '2016-07-16': { price: 26800, count: 0 },
        '2016-07-17': { price: 26850, count: 0 },
        '2016-07-18': { price: 29100, count: 0 },
        '2016-07-19': { price: 28000, count: 0 },
        '2016-07-20': { price: 27150, count: 0 },
        '2016-07-21': { price: 25933, count: 0 },
        '2016-07-23': { price: 26700, count: 0 },
        '2016-07-24': { price: 26499, count: 0 },
        '2016-07-25': { price: 26400, count: 0 },
        '2016-07-26': { price: 22388, count: 0 },
        '2016-07-27': { price: 22222, count: 0 },
        '2016-07-28': { price: 22000, count: 0 },
        '2016-07-29': { price: 23175, count: 0 },
        '2016-07-30': { price: 23500, count: 0 },
        '2016-07-31': { price: 23916, count: 0 },
        '2016-08-01': { price: 25800, count: 0 },
        '2016-08-02': { price: 25200, count: 0 },
        '2016-08-04': { price: 22750, count: 0 },
        '2016-08-07': { price: 20625, count: 0 },
        '2016-08-08': { price: 24792, count: 0 },
        '2016-08-09': { price: 21500, count: 0 }
    }
}

我知道您可以通过访问JSON数据。 (句号)所以,我试过做body.history.2016-06-23.price,但后来我得到了这个错误

C:\Users\Richard\Desktop\Steam\NodeJS\PriceCompareBot\test.js:11
    console.log(body.history.2016-06-23.price);
                     ^^^^^^^

SyntaxError: missing ) after argument list
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:374:25)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Function.Module.runMain (module.js:442:10)
at startup (node.js:136:18)
at node.js:966:3

所以我相信它会在看到一个整数时停止吗?我如何通过这个,并访问日期的json价格数据?我敢肯定,我忘记了一件小事,但我不知道它是什么。谢谢。

1 个答案:

答案 0 :(得分:-2)

使用。访问成员。被称为点符号。使用[]访问它们称为括号表示法。

点符号仅适用于作为有效标识符名称的属性名称,因此基本上任何名称也是有效的变量名称,2016-06-14不是有效的变量名称。

You can use body.history['2016-06-14'];