简体   繁体   English

访问json文件中的数组值

[英]Accessing Array Values in a json file

I have this json file that I am appending to the html. 我有要附加到html的这个json文件。

"{\"Item1\":[{\"Id\":2,\"Title\":\"Support\",\"Items\":8},{\"Id\":5,\"Title\":\"Datacenter\",\"Items\":5},{\"Id\":3,\"Title\":\"Bogholderiet\",\"Items\":5},{\"Id\":8,\"Title\":\"Helpdesk\",\"Items\":4},{\"Id\":9,\"Title\":\"SLA og VIP\",\"Items\":1},{\"Id\":7,\"Title\":\"Hostmaster\",\"Items\":1}],\"Item2\":[{\"Id\":7,\"FullName\":\"p11\",\"Items\":5},{\"Id\":17,\"FullName\":\"p8\",\"Items\":3},{\"Id\":9,\"FullName\":\"p10\",\"Items\":3},{\"Id\":8,\"FullName\":\"p6\",\"Items\":3},{\"Id\":3,\"FullName\":\"p1\",\"Items\":3},{\"Id\":5,\"FullName\":\"p2\",\"Items\":2},{\"Id\":16,\"FullName\":\"p3\",\"Items\":1},{\"Id\":11,\"FullName\":\"p4\",\"Items\":1}]}"

My question is how can I append only the value of "SLA og VIP". 我的问题是如何仅附加“ SLA og VIP”的值。 Below code I am able to access to array of Item1 and appending it to html. 下面的代码,我能够访问Item1数组并将其附加到html。

if (key === 'Item1') {
value[key].forEach(function (val) {
var tbl3Row = "<tr " + (parseInt(val.TotalUnresolvedItems) > 3 ? " class='colorgul'" : "") + (parseInt(val.TotalUnresolvedItems) < 4 ? " class='colorgreen'" : "") + ">" + "<td>" + val.Title + "</td>" + "<td>" + val.TotalUnresolvedItems + "</td>" + "</tr>"
table3Rows += tbl3Row;                                   
 })
 }

But now I am only trying to access to the Title value"SLA og VIP". 但是现在我只尝试访问标题值“ SLA og VIP”。 This is the formula that i manage to find: 这是我设法找到的公式:

parsedData.Item1[4]

I just dont know how can I use this inside of this code: 我只是不知道如何在代码内使用此代码:

if (key === 'Item1') {
value[key].forEach(function (val) {
var tbl3Row = "<tr " + (parseInt(val.TotalUnresolvedItems) > 3 ? " class='colorgul'" : "") + (parseInt(val.TotalUnresolvedItems) < 4 ? " class='colorgreen'" : "") + ">" + "<td>" + val.Title + "</td>" + "<td>" + val.TotalUnresolvedItems + "</td>" + "</tr>"
table3Rows += tbl3Row;                                   
 })
 }

You can [].prototype.filter() it: 您可以[].prototype.filter()它:

Because it will let you filter to the exact object you need to target. 因为它可以过滤您需要定位的确切对象。 In your case it is 你的情况是

{
  "Id": 9,
  "Title": "SLA og VIP",
  "Items": 1
}

With this you can get the filtered object's properties like Id, Title, Items . 通过此操作,您可以获取过滤后的对象的属性,例如Id, Title, Items

if (key === 'Item1') {
  var obj = value[key].filter(function(item){
     return item.Title === "SLA og VIP"
  })[0];

  // now use obj.Title, output: "SLA og VIP"

  value[key].forEach(function(val) {
    var tbl3Row = "<tr " + (parseInt(val.TotalUnresolvedItems) > 3 ? "...</tr>"
    table3Rows += tbl3Row;
  })
}

 var json = "{\\"Item1\\":[{\\"Id\\":2,\\"Title\\":\\"Support\\",\\"Items\\":8},{\\"Id\\":5,\\"Title\\":\\"Datacenter\\",\\"Items\\":5},{\\"Id\\":3,\\"Title\\":\\"Bogholderiet\\",\\"Items\\":5},{\\"Id\\":8,\\"Title\\":\\"Helpdesk\\",\\"Items\\":4},{\\"Id\\":9,\\"Title\\":\\"SLA og VIP\\",\\"Items\\":1},{\\"Id\\":7,\\"Title\\":\\"Hostmaster\\",\\"Items\\":1}],\\"Item2\\":[{\\"Id\\":7,\\"FullName\\":\\"p11\\",\\"Items\\":5},{\\"Id\\":17,\\"FullName\\":\\"p8\\",\\"Items\\":3},{\\"Id\\":9,\\"FullName\\":\\"p10\\",\\"Items\\":3},{\\"Id\\":8,\\"FullName\\":\\"p6\\",\\"Items\\":3},{\\"Id\\":3,\\"FullName\\":\\"p1\\",\\"Items\\":3},{\\"Id\\":5,\\"FullName\\":\\"p2\\",\\"Items\\":2},{\\"Id\\":16,\\"FullName\\":\\"p3\\",\\"Items\\":1},{\\"Id\\":11,\\"FullName\\":\\"p4\\",\\"Items\\":1}]}"; var obj = JSON.parse(json)["Item1"].filter(function(item) { return item.Title === "SLA og VIP" })[0]; console.log(obj); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM