用于解析json对象的Javascript

时间:2017-10-26 04:13:37

标签: javascript json

我正在尝试使用javascript解析以下(经过验证的)json字符串。我已经尝试了以下代码,但它似乎并没有起作用。

var obj = JSON.parse(data);
alert(obj.Design_Lump_Sum[1]);
alert(obj[1]);

json对象:

     {
    "Capex_Lump_Sum": 
    {
    "Pipework and associated fittings": "86086.9",
    "Cable Trays": "10410.4",
    "New Mechanical Services Switchboard": "23209.28",
    "Decommissioning removal of Redundant Mechanical Plant": "28342.32",
    "Electrical system labelling": "473.2",
    "Handover Documentation": "2957.5",
    "Mechanical system labelling": "946.4",
    "Service and Maintenance during DL": "16196.46",
    "Painting": "9227.4",
    "Submain Cabling": "27471.63",
    "Construction Works": "17464.63",
    "Decommissioning removal of redundant mech electrical plant": "5323.5",
    "Framed AsBuilt Drawings": "591.5",
    "Pre Post Air Balancing": "10883.6",
    "Innotech DDC controls and associated cabling": "23831.54",
    "LSL": "2663.0",
    "Testing and Commissioning": "4519.06",
    "Temporary air conditioning": "87243.88",
    "Supply and installation of Air Handling Units Condensor Units": "194521.85",
    "133.0": "105.0",
    "Ductwork Dampers Louvres": "96018.19",
    "Building certifier": "7038.85",
    "Facility Manager Training": "1183.0",
    "AMS": "5915.0",
    "Pre Post Site Acoustic Testing": "5867.68",
    "Penetrations and Fire Sealing": "3903.9"
    },

    "Design_Lump_Sum": 
    {
    "Electrical Services Design": "6915.82",
    "Mechanical Services Design": "13569.01"
    }, 

    "Opex_Lump_Sum": 
    {"Testing and Commissioning": "1200.0"}
}

1 个答案:

答案 0 :(得分:1)

由于它已经是有效的JSON ,因此您无需解析它。只需删除该行 - var obj = JSON.parse(data);并直接访问您的对象 - obj.Design_Lump_Sum['Electrical Services Design']