javascript - 包含括号作为键值的 JSON 文件
问题描述
我目前有一个 json 文件,如下所示:
{
"Inventory": [{
"id": "1",
"StockNumber": "1000",
"Make": "Ford",
"Model": "F150",
"Trim": "STX",
"Year": "2011",
"Color": "Red",
"Cylinders": "8",
"Transmission": "Automatic"
"Gear_Ratio_(4x4)": "",
}, {
"id": "2",
"StockNumber": "1001",
"Make": "Ford",
"Model": "F150",
"Trim": "Lariat",
"Year": "2012",
"Color": "Black",
"Cylinders": "8",
"Transmission": "Automatic"
"Gear_Ratio_(4x4)": "4.56",
}, {
"id": "3",
"StockNumber": "1002",
"Make": "Chevy",
"Model": "Silverato",
"Trim": "",
"Year": "2020",
"Color": "Red",
"Cylinders": "8",
"Transmission": "Automatic"
"Gear_Ratio_(4x4)": "4.56",
}]
}
我的问题是,我无法获得他们键 Gear_Ratio_(4x4) 的值。
我努力了:
$.getJSON(dataFile, function (data) {
$.each(data.Inventory, function (index, value) {
if (value.StockNumber === $("#productStockNumber option:selected").text()) {
console.log("The Make is: " + value.Make);
console.log("4x4 Gear ratio is: " + value.Gear_Ratio_(4x4));
}
});
});
这有效:console.log("Make 是:" + value.Make);
这不起作用:console.log("4x4 齿轮比是:" + value.Gear_Ratio_(4x4)); 给出错误:Uncaught SyntaxError: Invalid or unexpected token
我试图逃避括号,但这没有用:
console.log("4x4 齿轮比为:" + value.Gear_Ratio_\(4x4\));
更改 JSON 可能不是一种选择,因为还有其他包含括号的键,例如零件的主体尺寸等。
对不起,如果我自己错过了解决方案。谢谢您的帮助。
解决方案
推荐阅读
- ios - iOS 应用程序中的按钮在 VM 上表现异常
- angular - 如何在经过消毒的输入上书写?
- android - 如何删除相机焦点上的锚节点
- ms-access - 打开表单并将未绑定的 ComboBox 设置为特定值
- javascript - 将颜色更改应用于图像作为叠加层,保持原始纹理/着色器 Javascript
- python - SESSION_COOKIE_SECURE=Django 2.2 中的 True 将管理员登录重定向到自身
- sql - 两个具有相同内容的表会破坏数据规范化吗?
- java - 查找最小长度为 2 个字符的所有常见子字符串
- ms-access - 错误 3464:使用 Allen Browne 的 Concat 函数的条件表达式中的数据类型不匹配
- c++ - 如何使用 Shader Storage Buffer 和 std430 建立 glBindBufferRange() 偏移量?