如何使用 javascript 中的此键编写代码来提取值?

How can I write code using this key in javascript to extract the value?

这是我通过 API 获取的 data:

{ "0x5af2be193a6abca9c8817001f45744777db30756": { "usd": 0.11039 }}

但是当我尝试访问要放入 html 的数据时,使用:

var price = data.0x5af2be193a6abca9c8817001f45744777db30756.usd;

错误读回:

Uncaught SyntaxError: Invalid or unexpected token

我认为是因为它无法识别 0x5af2be193a6abca9c8817001f45744777db30756
作为一个字符串,但我不知道如何修复它。

试试下面的代码:

var price = data["0x5af2be193a6abca9c8817001f45744777db30756"].usd;

根据 JavaScript MDN 文档 Objects and Properties

"An object property name can be any valid JavaScript string, or anything that can be converted to a string, including the empty string. However, any property name that is not a valid JavaScript identifier (for example, a property name that has a space or a hyphen, or that starts with a number) can only be accessed using the square bracket notation."

您的密钥“0x5af2be193a6abca9c8817001f45744777db30756”是一个无效标识符,因为它以数字开头。您可以使用方括号表示法访问它:

var price = data["0x5af2be193a6abca9c8817001f45744777db30756"].usd;