如何在没有密钥的javascript的情况下解析嵌套的json

时间:2016-05-05 10:50:17

标签: javascript arrays json

我想在没有任何密钥的情况下解析嵌套的json数组,使用JavaScript请帮忙。

Json数据:

[
["S. No.", "Bank Name", "First 4 Letters of ", "IFSC", "Short Code", "Multimodal Code"],
["1", "Abhyudaya Co-op Bank", "ABHY", "ACB", "*99*87#"],
["2", "Allahabad Bank", "ALLA", "ALB", "*99*54#"],
["3", "Andhra Bank", "ANDB", "ANB", "*99*59#"],
["4", "Apna Sahakari Bank", "ASBL", "APN", "*99*85#"],
["5", "Axis Bank", "UTIB", "AXB", "*99*45#"],
["6", "Bank of Baroda", "BARB", "BOB", "*99*48#"],
["7", "Bank of India", "BKID", "BOI", "*99*47#"],
["8", "Bank of Maharashtra", "MAHB", "BOM", "*99*61#"],
["9", "Bhartiya Mahila Bank", "BMBL", "BMB", "*99*86#"],
["10", "Canara Bank", "CNRB", "CNB", "*99*46#"],
["11", "Central Bank of India", "CBIN", "CBI", "*99*51#"],
["12", "Corporation Bank", "CORP", "CRB", "*99*57#"],
["13", "DCB Bank", "DCBL", "DCB", "*99*65#"],
["14", "Dena Bank", "BKDN", "DNB", "*99*78#"],
["15", "Federal Bank", "FDRL", "FBL", "*99*72#"],
["16", "Gujarat State Co-op Bank", "GSCB", "GSC", "*99*90#"],
["17", "Hasti Co-op Bank", "HCBL", "HCB", "*99*89#"],
["18", "HDFC Bank", "HDFC", "HDF", "*99*43#"],
["19", "ICICI Bank", "ICICI", "ICI", "*99*44#"],
["20", "IDBI Bank", "IBKL", "IDB", "*99*49#"],
["21", "Indian Bank", "IDIB", "INB", "*99*58#"],
["22", "Indian Overseas Bank", "IOBA", "IOB", "*99*52#"],
["23", "IndusInd Bank", "INDB", "IIB", "*99*69#"],
["24", "ING Vysya Bank", "VYSA", "ING", "NA"],
["25", "Janata Sahakari Bank", "JSBP", "JSB", "*99*81#"]
]

这是json格式。我想获得银行名称,例如" Abhyudaya Co-op Bank"," Allahabad Bank" ,但不要解析第一个数组:

["S. No.", "Bank Name", "First 4 Letters of ", "IFSC", "Short Code", "Multimodal Code"],

请帮忙。

4 个答案:

答案 0 :(得分:0)

你所拥有的是array arrays并且你需要遍历主数组(忽略索引0,因为你不需要它)并且在循环内只访问第一个索引值,因为这有银行名称



var data = [
["S. No.", "Bank Name", "First 4 Letters of ", "IFSC", "Short Code", "Multimodal Code"],
["1", "Abhyudaya Co-op Bank", "ABHY", "ACB", "*99*87#"],
["2", "Allahabad Bank", "ALLA", "ALB", "*99*54#"],
["3", "Andhra Bank", "ANDB", "ANB", "*99*59#"],
["4", "Apna Sahakari Bank", "ASBL", "APN", "*99*85#"],
["5", "Axis Bank", "UTIB", "AXB", "*99*45#"],
["6", "Bank of Baroda", "BARB", "BOB", "*99*48#"],
["7", "Bank of India", "BKID", "BOI", "*99*47#"],
["8", "Bank of Maharashtra", "MAHB", "BOM", "*99*61#"],
["9", "Bhartiya Mahila Bank", "BMBL", "BMB", "*99*86#"],
["10", "Canara Bank", "CNRB", "CNB", "*99*46#"],
["11", "Central Bank of India", "CBIN", "CBI", "*99*51#"],
["12", "Corporation Bank", "CORP", "CRB", "*99*57#"],
["13", "DCB Bank", "DCBL", "DCB", "*99*65#"],
["14", "Dena Bank", "BKDN", "DNB", "*99*78#"],
["15", "Federal Bank", "FDRL", "FBL", "*99*72#"],
["16", "Gujarat State Co-op Bank", "GSCB", "GSC", "*99*90#"],
["17", "Hasti Co-op Bank", "HCBL", "HCB", "*99*89#"],
["18", "HDFC Bank", "HDFC", "HDF", "*99*43#"],
["19", "ICICI Bank", "ICICI", "ICI", "*99*44#"],
["20", "IDBI Bank", "IBKL", "IDB", "*99*49#"],
["21", "Indian Bank", "IDIB", "INB", "*99*58#"],
["22", "Indian Overseas Bank", "IOBA", "IOB", "*99*52#"],
["23", "IndusInd Bank", "INDB", "IIB", "*99*69#"],
["24", "ING Vysya Bank", "VYSA", "ING", "NA"],
["25", "Janata Sahakari Bank", "JSBP", "JSB", "*99*81#"]
];


for(var i=1; i< data.length; i++){  //neglect 0 index
   console.log(data[i][1]);         //access the first index, as it has the bank name
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

如果你在字符串中有这个JSON,那么使用JSON.parse来获取数组

var arr = JSON.parse( str ); //convert to array object if input is a string
var bankNames = arr.map(function(val){ return val[1] }); //get only bankname second index of each row
bankNames.splice(0,1); //remove first row result

如果输入已经是数组,则忽略第一个语句。

答案 2 :(得分:0)

因此,银行名称始终是数组中的第二个元素。

// skip first line as it's header.
for (var i = 1; i < banksArray.length; i++) {
    console.log(banksArray[i][1]); // "Abhyudaya Co-op Bank"
}

答案 3 :(得分:0)

这是您的getBanks()功能。

var bdata = [
["S. No.", "Bank Name", "First 4 Letters of ", "IFSC", "Short Code", "Multimodal Code"],
["1", "Abhyudaya Co-op Bank", "ABHY", "ACB", "*99*87#"],
["2", "Allahabad Bank", "ALLA", "ALB", "*99*54#"],
["3", "Andhra Bank", "ANDB", "ANB", "*99*59#"],
["4", "Apna Sahakari Bank", "ASBL", "APN", "*99*85#"],
["5", "Axis Bank", "UTIB", "AXB", "*99*45#"],
["6", "Bank of Baroda", "BARB", "BOB", "*99*48#"],
["7", "Bank of India", "BKID", "BOI", "*99*47#"],
["8", "Bank of Maharashtra", "MAHB", "BOM", "*99*61#"],
["9", "Bhartiya Mahila Bank", "BMBL", "BMB", "*99*86#"],
["10", "Canara Bank", "CNRB", "CNB", "*99*46#"],
["11", "Central Bank of India", "CBIN", "CBI", "*99*51#"],
["12", "Corporation Bank", "CORP", "CRB", "*99*57#"],
["13", "DCB Bank", "DCBL", "DCB", "*99*65#"],
["14", "Dena Bank", "BKDN", "DNB", "*99*78#"],
["15", "Federal Bank", "FDRL", "FBL", "*99*72#"],
["16", "Gujarat State Co-op Bank", "GSCB", "GSC", "*99*90#"],
["17", "Hasti Co-op Bank", "HCBL", "HCB", "*99*89#"],
["18", "HDFC Bank", "HDFC", "HDF", "*99*43#"],
["19", "ICICI Bank", "ICICI", "ICI", "*99*44#"],
["20", "IDBI Bank", "IBKL", "IDB", "*99*49#"],
["21", "Indian Bank", "IDIB", "INB", "*99*58#"],
["22", "Indian Overseas Bank", "IOBA", "IOB", "*99*52#"],
["23", "IndusInd Bank", "INDB", "IIB", "*99*69#"],
["24", "ING Vysya Bank", "VYSA", "ING", "NA"],
["25", "Janata Sahakari Bank", "JSBP", "JSB", "*99*81#"]
];

var getBanks = (b, t = b.shift()) => b.map(e => e[1]);

document.write("<pre>" + JSON.stringify(getBanks(bdata), null,2) + "</pre>");