如何在Json对象中打印数组

时间:2017-06-14 01:58:18

标签: javascript json

我得到了这个Json String

{ "Banks": [ 
  { "BankNo": "TEST 5", "Name": "MAY", "Account": "987654", "BankCode": "ICBK", "Branch": "MAY" }, 
  { "BankNo": "TEST 6", "Name": "PETER", "Account": "987653", "BankCode": "ABOC", "Branch": "PETER" }, 
  { "BankNo": "TEST 7", "Name": "DAVID", "Account": "987652", "BankCode": "PCBC", "Branch": "DAVID" }, 
  { "BankNo": "TEST 8", "Name": "JOHN", "Account": "987651", "BankCode": "CMBC", "Branch": "JOHN" } 
] }

我尝试了很多方法将其打印出来但却失败了......我如何在这个Json中逐个循环?

我预期的结果是 第一圈打印出来 BankNo:TEST 5

2nd Loop打印出来 BankNo:TEST 6

1 个答案:

答案 0 :(得分:0)

试试这个



var jn = { "Banks": [ { "BankNo": "TEST 5", "Name": "MAY", "Account": "987654", "BankCode": "ICBK", "Branch": "MAY" }, { "BankNo": "TEST 6", "Name": "PETER", "Account": "987653", "BankCode": "ABOC", "Branch": "PETER" }, { "BankNo": "TEST 7", "Name": "DAVID", "Account": "987652", "BankCode": "PCBC", "Branch": "DAVID" }, { "BankNo": "TEST 8", "Name": "JOHN", "Account": "987651", "BankCode": "CMBC", "Branch": "JOHN" } ] }

//getting banks array
var banks = jn.Banks

banks.forEach((bank)=> {
console.log(`${bank.BankNo}: ${bank.Name}`)
})