javascript - 在javascript中解析JSON数据

标签 javascript jquery json

我有以下 JSON 数据,我需要根据 JAVASCRIPT 中的 Keytype 迭代此数据。 它应该仅从传递的键代码返回记录。 假设我需要 438 的记录意味着它应该只给我以下数据。 (例如:“K”:“43800001”,“D”:“数据1”)

{
  "GroupCode": {
    "PickType": [
      {
        "@KeyType": "438",
        "R": [
          {
            "K": "43800001",
            "D": "Data1"
          }
        ]
      },
      {
        "@KeyType": "439",
        "R": [
          {
            "K": "43900001",
            "D": "Data2"
          }
        ]
      },
      {
        "@KeyType": "440",
        "R": [
          {
            "K": "44000001",
            "D": "Data3"
          }
        ]
      },
      {
        "@KeyType": "441",
        "R": [
          {
            "K": "44100001",
            "D": "Data4"
          }
        ]
      }
    ]
  }
}

由于我不擅长java脚本并且是新手,所以我还没有尝试为此编写代码。请帮我获取数据。

最佳答案

假设上面的内容存储在变量 obj 中。您可以通过以下方式获取结果

var result = obj.GroupCode.PickType.filter(function(item){
                return item["@KeyType"] === "438"
            }).map(function(item){
               return item.R[0];
            });

请注意结果是一个数组。如果您有符合条件的唯一对象,那么您必须检查数组的长度,然后提取对象,即 result[0]result.shift()

关于javascript - 在javascript中解析JSON数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35199913/

相关文章:

json - 如何将数据从 JSON 对象(在文件中)读入 R?

ios - NSJSONSerialization - 如何正确地将 JSON 转换为 NSArray?

javascript - jQuery UI 没有方法 'sortable'

javascript - 阻止 HTML Canvas 对象选择

jquery - "return this.each()"在 jQuery 中做什么?

javascript - 在 Chrome 扩展程序中,content_scripts 匹配被忽略

javascript - 在react和webpack中获取工作目录和文件名

javascript - 悬停时 Vanilla javascript classList操作

javascript - jquery 输入字段模式

jquery - 如何缩放多个 div 并留在容器内?