从嵌套的Json列表中提取值

时间:2017-11-23 02:08:28

标签: javascript json

如何让javascript读取下面json列表的“末尾”值。 我目前正在使用下面的代码,除了这段代码没有读取下面列表的最后3个值,格式不同,即

Json列表的摘录:

  

{......'Design_Lump_Sum':{0:{'**初步设计':'2366。 0' },   1:{'**详细设计':'15379.0'},2:{'** Const Stage Services':   '4732.0'}}}

代码:

var obj = JSON.parse(data);
var keys = Object.keys(obj.Design_Lump_Sum);
for (var asset in keys) {
    console.log(asset + " -> " + keys[asset] + " = " + obj.Design_Lump_Sum[keys[asset]]);
    }
var keys1 = Object.keys(obj.Capex_Lump_Sum);
for (var asset in keys1) {
    console.log(asset + " -> " + keys1[asset] + " = " + obj.Design_Lump_Sum[keys1[asset]]);
    }
var keys2 = Object.keys(obj.Opex_Lump_Sum);
for (var asset in keys2) {
    console.log(asset + " -> " + keys2[asset] + " = " + obj.Design_Lump_Sum[keys2[asset]]);
    }
var keys3 = Object.keys(obj.Provisional_Sum);
for (var asset in keys3) {
    console.log(asset + " -> " + keys3[asset] + " = " + obj.Design_Lump_Sum[keys3[asset]]);
    }
var keys4 = Object.keys(obj.Management_fees);
for (var asset in keys4) {
    console.log(asset + " -> " + keys4[asset] + " = " + obj.Design_Lump_Sum[keys4[asset]]);
    }
var keys5 = Object.keys(obj.CSA);
for (var asset in keys5) {
    console.log(asset + " -> " + keys5[asset] + " = " + obj.Design_Lump_Sum[keys5[asset]]);
    }

完整的JSON列表:

{  
  "Management_fees":{  },
  "Capex_Lump_Sum":{  
    "Refrigerant piping":"48040.447",
    "Preparation of Mops":"2137.681",
    "Labels":"653.016",
    "Penetrations and Protection":"1535.534",
    "AC Plinth and Trays":"5221.762",
    "Insulated Panel Windows":"6527.794",
    " MSSB":"19582.199",
    "Make Good Walls and Floors":"4154.696",
    "Controls":"24092.978",
    "** Building Surveyor Fee (Misc)":"7038.85",
    "Project Management, Supervision":"38447.5",
    "Painting":"9138.675",
    "Preliminaries":"1306.032",
    "Package system wiring":"16971.318",
    "Warranty":"2610.881",
    "Ductwork, Valves and Dampers":"77262.913",
    "Unit type":"89107.109",
    "Structural Engineer for new unit stands":"3916.913",
    "Manuals":"3823.456",
    "AMS and BMS works":"3916.913",
    "Accoustic Report":"4243.421",
    "Lighting":"5033.665",
    "Drawings":"3823.456",
    "Temp Cooling":"10443.524",
    "Commissioning":"3263.897",
    "Crane":"3003.637",
    "Demolition":"26455.429",
    "Fire detection and Alarm FIP modifications":"3263.897",
    "LSL":"1939.81059657",
    "Economy Cycle":"23169.055"
  },
  "Provisional_Sum":{  
    "Condenser Roof Platform":"20000.0",
    "Removal of ladder outside cable chamber exit stairs":"10000.0"
  },
  "Opex_Lump_Sum":{  },
  "CSA":{  
    "Additional Hendry Fees":"3742.5",
    "External Ladder":"10147.0",
    "Asbestos removal Works Total ($42,485.15) (remainder of cost $30K under PS sum)":"12485.15"
  },
  "Design_Lump_Sum":{  
    "0":{  
      "** Preliminary Design":"2366.0"
    },
    "1":{  
      "** Detailed Design":"15379.0"
    },
    "2":{  
      "** Const Stage Services":"4732.0"
    }
  },
  "Total":{  
    "sub_Total":"number:530154.0105965699"
  }
}

1 个答案:

答案 0 :(得分:0)

如果我没有误解你的问题,你可以尝试这样做。

var obj={'Management_fees':{},'Capex_Lump_Sum':{'Refrigerant piping':'48040.447','Preparation of Mops':'2137.681','Labels':'653.016','Penetrations and Protection':'1535.534','AC Plinth and Trays':'5221.762','Insulated Panel Windows':'6527.794',' MSSB':'19582.199','Make Good Walls and Floors':'4154.696','Controls':'24092.978','** Building Surveyor Fee (Misc)':'7038.85','Project Manag ement, Supervision':'38447.5','Painting':'9138.675','Preliminaries':'1306.032','Package system wiring':'16971.318','Warranty':'2610.881','Ductwork, Valves and Dampers':'77262.913','Unit ty pe':'89107.109','Structural Engineer for new unit stands':'3916.913','Manuals':'3823.456','AMS and BMS works':'3916.913','Accoustic Report':'4243.421','Lighting':'5033.665','Drawings':'38 23.456','Temp Cooling':'10443.524','Commissioning':'3263.897','Crane':'3003.637','Demolition':'26455.429','Fire detection and Alarm FIP modifications':'3263.897','LSL':'1939.81059657','Ec onomy Cycle':'23169.055'},'Provisional_Sum':{'Condenser Roof Platform':'20000.0','Removal of ladder outside cable chamber exit stairs':'10000.0'},'Opex_Lump_Sum':{},'CSA':{'Additional Hendry Fees':'3742.5','External Ladder':'10147.0','Asbestos removal Works Total ($42,485.15) (remainder of cost $30K under PS sum)':'12485.15'},'Design_Lump_Sum':{0:{'** Preliminary Design':'2366. 0'},1:{'** Detailed Design':'15379.0'},2:{'** Const Stage Services':'4732.0'}},'Total':{'sub_Total':'number:530154.0105965699'}};

var data = obj.Design_Lump_Sum;

Object.keys(data).map(function(item,index){  
  Object.keys(data[item]).map(function(key) {
   console.log(key, "->", data[item][key]);
  });
});