首页 > 解决方案 > 具有重复属性名称的嵌套 JSON 数组访问

问题描述

如何访问 JS 中的“屋顶绝缘”数组值?甚至可以使用重复的属性名称吗?

我尝试了 productListing['Roofing Insulation']['Roofing Insulation'] 和更多类型的语法,但没有成功。我知道重复是不好的做法,但这是我必须要做的一切......

{
  "storeServices":{
     "Local Services":[
        "Rooftop",
        "EPDM",
        "Next-Day Delivery Available"
     ],
     "National Services":[
        "Covered",
        "Window",
        "Job Site Signs"
     ]
  },
  "productListing":{
     "Roofing Insulation":{
        "Roofing Insulation":[
           "Celotex",
           "Dow",
           "Foamular",
           "Johns Manville",
           "Pactiv",
           "Tyvek"
        ]
     },
     "Other Products":{
        "Other Products":[
           "Caulks/Sealants",
           "Shutters",
           "Tools & Equipment",
           "Engineered Lumber",
           "Roofing & Siding Accessories"
        ]
     },
     "Low Slope Roofing":{
        "Low Slope Roofing":[
           "Johns Manville",
           "Mule-Hide Products",
           "Versico"
        ]
     },
     "Steep Slope Roofing":{
        "Concrete & Clay Roof Tiles":[
           "Vande Hey Raleigh"
        ],
        "Asphalt Shingles":[
           "CertainTeed",
           "GAF",
           "Owens Corning"
        ]
     },
     "Windows & Doors":{
        "Replacement Windows":[
           "Pella"
        ],
        "New Construction Windows":[
           "Pella"
        ],
        "Exterior Doors":[
           "Pella"
        ],
        "Skylights":[
           "Velux"
        ]
     },
     "Siding":{
        "Vinyl Siding":[
           "Mastic Home Exteriors"
        ]
     }
  },
  "branchDetails":{
     "branchNumber":"1"
  }
} 

标签: javascriptjson

解决方案


除非我们看到您尝试过的内容和您的代码,否则我们无能为力。如果您像下面这样访问它,它应该可以工作:

obj.productListing['Roofing Insulation']['Roofing Insulation']

下面是一个示例供您展示:

var obj = {
  "storeServices": {
    "Local Services": [
      "Rooftop",
      "EPDM",
      "Next-Day Delivery Available"
    ],
    "National Services": [
      "Covered",
      "Window",
      "Job Site Signs"
    ]
  },
  "productListing": {
    "Roofing Insulation": {
      "Roofing Insulation": [
        "Celotex",
        "Dow",
        "Foamular",
        "Johns Manville",
        "Pactiv",
        "Tyvek"
      ]
    },
    "Other Products": {
      "Other Products": [
        "Caulks/Sealants",
        "Shutters",
        "Tools & Equipment",
        "Engineered Lumber",
        "Roofing & Siding Accessories"
      ]
    },
    "Low Slope Roofing": {
      "Low Slope Roofing": [
        "Johns Manville",
        "Mule-Hide Products",
        "Versico"
      ]
    },
    "Steep Slope Roofing": {
      "Concrete & Clay Roof Tiles": [
        "Vande Hey Raleigh"
      ],
      "Asphalt Shingles": [
        "CertainTeed",
        "GAF",
        "Owens Corning"
      ]
    },
    "Windows & Doors": {
      "Replacement Windows": [
        "Pella"
      ],
      "New Construction Windows": [
        "Pella"
      ],
      "Exterior Doors": [
        "Pella"
      ],
      "Skylights": [
        "Velux"
      ]
    },
    "Siding": {
      "Vinyl Siding": [
        "Mastic Home Exteriors"
      ]
    }
  },
  "branchDetails": {
    "branchNumber": "1"
  }
};

document.getElementById("demo").innerHTML = obj.productListing['Roofing Insulation']['Roofing Insulation'];
<p id="demo"></p>


推荐阅读