【问题标题】:Check json data with javascript if a value exists如果存在值,则使用 javascript 检查 json 数据
【发布时间】:2020-07-09 13:31:17
【问题描述】:

我一直在尝试解析以下 json 文件(以及其他类似文件)以检查“地址”数据是否包含 [Address][Type] = 'Constituency'(或者 [Address][Postcode] 是否包含一个值 - 类似的问题)。

我尝试过对其使用过滤器功能,但无法得到任何接近工作的东西(并且已经搜索了听到答案并出现空白)。我尝试过使用香草 javascript 和 jquery(我都是初学者)。有什么建议吗?

JSON 数据样本 1:有选区地址 ...

{
  "Members": {
    "script": {
      "-id": "krispX",
      "-type": "text/javascript"
    },
    "Member": {
      "-Member_Id": "199",
      "-Dods_Id": "25747",
      "-Pims_Id": "2358",
      "-Clerks_Id": "77",
      "DisplayAs": "Mr abc def",
      "ListAs": "def, Ms abc",
      "FullTitle": "Mr abc def MP",
      "DateOfBirth": "1968-08-30T00:00:00",
      "DateOfDeath": {
        "-xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
        "-xsi:nil": "true"
      },
      "Gender": "M",
      "Party": {
        "-Id": "15",
        "#text": "My party"
      },
      "House": "Commons",
      "MemberFrom": "Westminster East",
      "HouseStartDate": "1997-05-01T00:00:00",
      "HouseEndDate": {
        "-xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
        "-xsi:nil": "true"
      },
      "CurrentStatus": {
        "-Id": "0",
        "-IsActive": "True",
        "Name": "Current Member",
        "StartDate": "2019-11-11T00:00:00"
      },
      "Addresses": {
        "Address": [
          {
            "-Type_Id": "6",
            "Type": "Website",
            "IsPreferred": "False",
            "IsPhysical": "False",
            "Address1": "http://www.abcdef.org.uk/"
          },
          {
            "-Type_Id": "4",
            "Type": "Constituency",
            "IsPreferred": "False",
            "IsPhysical": "True",
            "Address1": "My party",
            "Address2": "123 qwe rty",
            "Address5": "London",
            "Postcode": "W10 1DZ",
            "Phone": "123 456 7890",
            "Fax": "098 765 4321",
            "Email": "qwerty@asdfgh.uk"
          },
          {
            "-Type_Id": "1",
            "Type": "Parliamentary",
            "IsPreferred": "False",
            "IsPhysical": "True",
            "Address1": "House of Commons",
            "Address5": "London",
            "Postcode": "SW1A 0AA",
            "Phone": "123 456 7890",
            "Fax": "987 654 3210",
            "Email": "qwerty@asdfgh.uk"
          },
          {
            "-Type_Id": "7",
            "Type": "Twitter",
            "IsPreferred": "False",
            "IsPhysical": "False",
            "Address1": "https://twitter.com/qwerty"
          }
        ]
      }
    }
  }
}

...

JSON 数据示例 2:缺少选区地址但有邮政编码 ...

{
  "Members": {
    "script": {
      "-id": "krispX",
      "-type": "text/javascript"
    },
    "Member": {
      "-Member_Id": "199",
      "-Dods_Id": "25747",
      "-Pims_Id": "2358",
      "-Clerks_Id": "77",
      "DisplayAs": "Mr abc def",
      "ListAs": "def, Ms abc",
      "FullTitle": "Mr abc def MP",
      "DateOfBirth": "1968-08-30T00:00:00",
      "DateOfDeath": {
        "-xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
        "-xsi:nil": "true"
      },
      "Gender": "M",
      "Party": {
        "-Id": "15",
        "#text": "My party"
      },
      "House": "Commons",
      "MemberFrom": "Westminster East",
      "HouseStartDate": "1997-05-01T00:00:00",
      "HouseEndDate": {
        "-xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
        "-xsi:nil": "true"
      },
      "CurrentStatus": {
        "-Id": "0",
        "-IsActive": "True",
        "Name": "Current Member",
        "StartDate": "2019-11-11T00:00:00"
      },
      "Addresses": {
        "Address": [
          {
            "-Type_Id": "6",
            "Type": "Website",
            "IsPreferred": "False",
            "IsPhysical": "False",
            "Address1": "http://www.abcdef.org.uk/"
          },
          {
            "-Type_Id": "1",
            "Type": "Parliamentary",
            "IsPreferred": "False",
            "IsPhysical": "True",
            "Address1": "House of Commons",
            "Address5": "London",
            "Postcode": "SW1A 0AA",
            "Phone": "123 456 7890",
            "Fax": "987 654 3210",
            "Email": "qwerty@asdfgh.uk"
          },
          {
            "-Type_Id": "7",
            "Type": "Twitter",
            "IsPreferred": "False",
            "IsPhysical": "False",
            "Address1": "https://twitter.com/qwerty"
          }
        ]
      }
    }
  }
}

...

已添加:JSON 数据示例 3:有选区但缺少邮政编码(= 没有邮政地址) ...

{
  "Members": {
    "script": {
      "-id": "krispX",
      "-type": "text/javascript"
    },
    "Member": {
      "-Member_Id": "199",
      "-Dods_Id": "25747",
      "-Pims_Id": "2358",
      "-Clerks_Id": "77",
      "DisplayAs": "Mr abc def",
      "ListAs": "def, Ms abc",
      "FullTitle": "Mr abc def MP",
      "DateOfBirth": "1968-08-30T00:00:00",
      "DateOfDeath": {
        "-xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
        "-xsi:nil": "true"
      },
      "Gender": "M",
      "Party": {
        "-Id": "15",
        "#text": "My party"
      },
      "House": "Commons",
      "MemberFrom": "Westminster East",
      "HouseStartDate": "1997-05-01T00:00:00",
      "HouseEndDate": {
        "-xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
        "-xsi:nil": "true"
      },
      "CurrentStatus": {
        "-Id": "0",
        "-IsActive": "True",
        "Name": "Current Member",
        "StartDate": "2019-11-11T00:00:00"
      },
      "Addresses": {
        "Address": [
          {
            "-Type_Id": "6",
            "Type": "Website",
            "IsPreferred": "False",
            "IsPhysical": "False",
            "Address1": "http://www.abcdef.org.uk/"
          },
          {
            "-Type_Id": "4",
            "Type": "Constituency",
            "IsPreferred": "False",
            "IsPhysical": "True",
            "Phone": "123 456 7890",
            "Email": "qwerty@asdfgh.uk"
          },
          {
            "-Type_Id": "1",
            "Type": "Parliamentary",
            "IsPreferred": "False",
            "IsPhysical": "True",
            "Address1": "House of Commons",
            "Address5": "London",
            "Postcode": "SW1A 0AA",
            "Phone": "123 456 7890",
            "Fax": "987 654 3210",
            "Email": "qwerty@asdfgh.uk"
          },
          {
            "-Type_Id": "7",
            "Type": "Twitter",
            "IsPreferred": "False",
            "IsPhysical": "False",
            "Address1": "https://twitter.com/qwerty"
          }
        ]
      }
    }
  }
}

...

[已编辑]

我根据 Jumshud 的回答找到了一个适用于上述所有三个数据示例的解决方案。

一旦我加载它(当然) jsondata = JSON.stringify(mydata); // 添加这个是因为数据来自 php 调用,没有它就无法工作 jsondata = JSON.parse(jsondata);

我将 JumshudSuggestio 改编为: const hasConstituencyAdrs = jsondata.Members.Member.Addresses.Address.some(address => address.Type === 'Constituency' && address.Postcode && address.Postcode.length > 0);

如果它有一个 type=constituency 的地址键并且它还有一个邮政编码键并且如果邮政编码键的长度大于 0,则返回 true。这将返回一个我现在可以使用的 true/false。

感谢大家的回答 - 我已经为此苦苦挣扎了 2 天,而您在一小时内帮助我解决了问题。

【问题讨论】:

  • 你能把你目前所做的放在你的 js 代码中吗?
  • A.RAZIK:抱歉 - 我尝试并丢弃了很多东西,以至于我没有一个我什至有信心接近的代码片段。我的目标是为下次保留一些代码!

标签: javascript json


【解决方案1】:

这将有助于找出数据样本 1 中是否存在任何具有选区类型的地址。将某些方法返回值设置为 bool 类型:

const data1 = JSON.parse(Data sample 1);
const hasConstituencyType = data1.Members.Member.Addresses.Address.some(address => address.Type === 'Constituency')

hasConstituencyType 的输出将是 true

为了获得带有邮政编码值的地址,请使用:

const adresses = data2.Members.Member.Addresses.Address.filter(address => address.Postcode && address.Postcode.length > 0)

【讨论】:

    【解决方案2】:

    您可以将dot-object 库与get(obj, path, defaultValue) 方法一起使用

    【讨论】:

      【解决方案3】:

      您可以将任何 json 字符串解析为可用的 JSON var,如下所示:

      var json = JSON.parse(jsonString);
      

      如果您想从 json 变量中获取任何内容,可以执行以下操作:

      var type = json.Members.Member.Addresses.Address[0].Type;
      

      我希望这有效! 你也可以通读这个: https://www.w3schools.com/js/js_json_objects.asp

      【讨论】:

      • 谢谢 Jesse - 我也会深入研究!
      【解决方案4】:

      当您想从.json 文件中获取数据时,您可以在JavaScript 中使用fetch API 来获取数据并应用逻辑。以下显示了一种非常简单的方法来检查选区类型:

      
      fetch("./data.json")
        .then((result) => {
          return result.json();
        })
        .then((resp) => {
          let jsonData = resp;
          console.log(jsonData);
          const addresses = jsonData.Members.Member.Addresses.Address;
          const check = addresses.filter(element => element.Type === "Constituency");
          if (check) {
            // yes Constituency type is found
          } else {
            // not found
          }
        });
      
      

      假设 data.json 包含你需要的数据

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-24
        • 1970-01-01
        • 2011-12-26
        • 1970-01-01
        • 1970-01-01
        • 2022-06-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多