如果值存在,请使用javascript检查json数据



我一直在尝试解析以下json文件(以及其他类似文件(,以检查"Addresses"数据是否包含[Address][Type]='Constituency'(或者[Address][Postcode]是否有类似的值问题(。

我试过在上面使用过滤功能,但一直没能找到任何接近工作的东西(我在hear上搜索了答案,结果都是空白的(。我尝试过使用vanilla 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.stringfy(mydata(;//添加此项是因为数据来自php调用,没有它就无法工作jsondata=JSON.parse(jsondata(;

我将Jumshud suggestio改编为:const hasConstituencyAdrs=jsondata。成员。成员。地址。地址。some(地址=>地址。类型==="选区"&&地址。邮编&地址。邮政编码。长度>0(;

如果它有一个类型为=选区的地址密钥,并且还有一个邮政编码密钥,并且邮政编码密钥的长度大于0,则返回true。这将返回一个我现在可以使用的真/假。

感谢大家的回答——我已经为此挣扎了两天,你在不到一个小时的时间里帮我解决了这个问题。

这将有助于找出数据样本1是否有任何具有Constituency Type的Address。数组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)

您可以使用dot-object库及其get(obj, path, defaultValue)方法

您可以将任何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

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


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包含您需要的数据

最新更新