【发布时间】: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