JSON:如果具有特定值,则获取json数组

时间:2018-10-01 10:38:18

标签: javascript json

我有一个带有数据的JSON对象:

{
  "ROOT": {
    "CATEGORY_AREA_LIST": {
      "CATEGORY_AREA": [{
        "@attributes": {
          "CATEGORY_AREA_NAME": "General"
        },
        "CATEGORY_TYPE": [{
          "@attributes": {
            "CATEGORY_TYPE_NAME": "MOC"
          },
          "CATEGORY_LOCATION": [{
            "@attributes": {
              "CATEGORY_LOCATION_NAME": "Location1",
              "PROPOSED_LEVEL": "1"
            }
          }, {
            "@attributes": {
              "CATEGORY_LOCATION_NAME": "Location2",
              "PROPOSED_LEVEL": "2"
            }
          }, {
            "@attributes": {
              "CATEGORY_LOCATION_NAME": "Location3",
              "PROPOSED_LEVEL": "3"
            }
          }, {
            "@attributes": {
              "CATEGORY_LOCATION_NAME": "Location4",
              "PROPOSED_LEVEL": "4"
            }
          }]
        }, {
          "@attributes": {
            "CATEGORY_TYPE_NAME": "LPG"
          },
          "CATEGORY_LOCATION": [{
            "@attributes": {
              "CATEGORY_LOCATION_NAME": "Location20",
              "PROPOSED_LEVEL": "11"
            }
          }, {
            "@attributes": {
              "CATEGORY_LOCATION_NAME": "Location22",
              "PROPOSED_LEVEL": "21"
            }
          }, {
            "@attributes": {
              "CATEGORY_LOCATION_NAME": "Location222",
              "PROPOSED_LEVEL": "13"
            }
          }, {
            "@attributes": {
              "CATEGORY_LOCATION_NAME": "Location2222",
              "PROPOSED_LEVEL": "41"
            }
          }]
        }]
      }, {
        "@attributes": {
          "CATEGORY_AREA_NAME": "Restricted"
        },
        "CATEGORY_TYPE": [{
          "@attributes": {
            "CATEGORY_TYPE_NAME": "DND"
          },
          "CATEGORY_LOCATION": [{
            "@attributes": {
              "CATEGORY_LOCATION_NAME": "L1",
              "PROPOSED_LEVEL": "11"
            }
          }, {
            "@attributes": {
              "CATEGORY_LOCATION_NAME": "L2",
              "PROPOSED_LEVEL": "22"
            }
          }, {
            "@attributes": {
              "CATEGORY_LOCATION_NAME": "L3",
              "PROPOSED_LEVEL": "33"
            }
          }, {
            "@attributes": {
              "CATEGORY_LOCATION_NAME": "L4",
              "PROPOSED_LEVEL": "44"
            }
          }]
        }, {
          "@attributes": {
            "CATEGORY_TYPE_NAME": "MNN"
          },
          "CATEGORY_LOCATION": [{
            "@attributes": {
              "CATEGORY_LOCATION_NAME": "Loc33",
              "PROPOSED_LEVEL": "8"
            }
          }, {
            "@attributes": {
              "CATEGORY_LOCATION_NAME": "Loc333",
              "PROPOSED_LEVEL": "7"
            }
          }, {
            "@attributes": {
              "CATEGORY_LOCATION_NAME": "Loc33333",
              "PROPOSED_LEVEL": "6"
            }
          }, {
            "@attributes": {
              "CATEGORY_LOCATION_NAME": "Loc333333",
              "PROPOSED_LEVEL": "5"
            }
          }]
        }]
      }]
    }
  }
}

我需要获取CATEGORY_AREA对象,该对象具有属性CATEGORY_AREA_NAME = "General"

我尝试使用filter / find函数,但没有得到想要的结果。

解决方案:

  

var jsArray = JSON.parse(jsonObj); var结果=   jsArray [“ ROOT”] [“ CATEGORY_AREA_LIST”] [“ CATEGORY_AREA”]。find(current => {         if(current [“ @ attributes”] [“ CATEGORY_AREA_NAME”] ==“常规”)             返回电流});

3 个答案:

答案 0 :(得分:1)

您可以按照我的方法进行操作

var obj = {
  "ROOT": {
    "CATEGORY_AREA_LIST": {
      "CATEGORY_AREA": [
        {
          "@attributes": {
            "CATEGORY_AREA_NAME": "General"
          },
          "CATEGORY_TYPE": [
            {
              "@attributes": {
                "CATEGORY_TYPE_NAME": "MOC"
              },
              "CATEGORY_LOCATION": [
                {
                  "@attributes": {
                    "CATEGORY_LOCATION_NAME": "Location1",
                    "PROPOSED_LEVEL": "1"
                  }
                },
                {
                  "@attributes": {
                    "CATEGORY_LOCATION_NAME": "Location2",
                    "PROPOSED_LEVEL": "2"
                  }
                },
                {
                  "@attributes": {
                    "CATEGORY_LOCATION_NAME": "Location3",
                    "PROPOSED_LEVEL": "3"
                  }
                },
                {
                  "@attributes": {
                    "CATEGORY_LOCATION_NAME": "Location4",
                    "PROPOSED_LEVEL": "4"
                  }
                }
              ]
            },
            {
              "@attributes": {
                "CATEGORY_TYPE_NAME": "LPG"
              },
              "CATEGORY_LOCATION": [
                {
                  "@attributes": {
                    "CATEGORY_LOCATION_NAME": "Location20",
                    "PROPOSED_LEVEL": "11"
                  }
                },
                {
                  "@attributes": {
                    "CATEGORY_LOCATION_NAME": "Location22",
                    "PROPOSED_LEVEL": "21"
                  }
                },
                {
                  "@attributes": {
                    "CATEGORY_LOCATION_NAME": "Location222",
                    "PROPOSED_LEVEL": "13"
                  }
                },
                {
                  "@attributes": {
                    "CATEGORY_LOCATION_NAME": "Location2222",
                    "PROPOSED_LEVEL": "41"
                  }
                }
              ]
            }
          ]
        },
        {
          "@attributes": {
            "CATEGORY_AREA_NAME": "Restricted"
          },
          "CATEGORY_TYPE": [
            {
              "@attributes": {
                "CATEGORY_TYPE_NAME": "DND"
              },
              "CATEGORY_LOCATION": [
                {
                  "@attributes": {
                    "CATEGORY_LOCATION_NAME": "L1",
                    "PROPOSED_LEVEL": "11"
                  }
                },
                {
                  "@attributes": {
                    "CATEGORY_LOCATION_NAME": "L2",
                    "PROPOSED_LEVEL": "22"
                  }
                },
                {
                  "@attributes": {
                    "CATEGORY_LOCATION_NAME": "L3",
                    "PROPOSED_LEVEL": "33"
                  }
                },
                {
                  "@attributes": {
                    "CATEGORY_LOCATION_NAME": "L4",
                    "PROPOSED_LEVEL": "44"
                  }
                }
              ]
            },
            {
              "@attributes": {
                "CATEGORY_TYPE_NAME": "MNN"
              },
              "CATEGORY_LOCATION": [
                {
                  "@attributes": {
                    "CATEGORY_LOCATION_NAME": "Loc33",
                    "PROPOSED_LEVEL": "8"
                  }
                },
                {
                  "@attributes": {
                    "CATEGORY_LOCATION_NAME": "Loc333",
                    "PROPOSED_LEVEL": "7"
                  }
                },
                {
                  "@attributes": {
                    "CATEGORY_LOCATION_NAME": "Loc33333",
                    "PROPOSED_LEVEL": "6"
                  }
                },
                {
                  "@attributes": {
                    "CATEGORY_LOCATION_NAME": "Loc333333",
                    "PROPOSED_LEVEL": "5"
                  }
                }
              ]
            }
          ]
        }
      ]
    }
  }
};
var result = obj["ROOT"]["CATEGORY_AREA_LIST"]["CATEGORY_AREA"].find(current=>{
    if(current["@attributes"]["CATEGORY_AREA_NAME"] == "General")
        return current;
});
console.log(result);

答案 1 :(得分:0)

用对您的JSON的引用替换“数据”,即可做到这一点:

data.ROOT.CATEGORY_AREA_LIST.CATEGORY_AREA.forEach(i => {
    if (i["@attributes"].CATEGORY_AREA_NAME === "General") {
        console.log(i);
    }
});

答案 2 :(得分:0)

您首先将json转换为js对象:NoSuchElementException

然后过滤器:

var data = JSON.parse(<json string>)