首页 > 解决方案 > 包含括号作为键值的 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 可能不是一种选择,因为还有其他包含括号的键,例如零件的主体尺寸等。

对不起,如果我自己错过了解决方案。谢谢您的帮助。

标签: javascriptjsonkeyparentheses

解决方案


推荐阅读