【发布时间】:2021-01-12 19:42:11
【问题描述】:
json type :1
var x={
"address": {
"county": "abc",
"state_district": "asd",
"state": "test",
"country": "test1",
"country_code": "test"
}
}
Type:2
var x ={ "address": {
"suburb": "",
"city_district": "",
"city": "",
"county": "",
"state": "",
"postcode": "",
"country": "",
}}
type:3
var x= {"address": {
"amenity": "",
"road": "",
"town": "",
"county": "",
"state_district": "",
"state": "",
"postcode": "",
"country": "",
"country_code": ""
}
}
switch(x.address)
{
case 'city':
return x.address.city;
break;
case 'village':
return x.address.village;
break;
default:
alert('err');}
这是我的 JSON 内容。 address 属性将具有一些额外的键值,具体取决于位置,例如 city、village、suburb 等。
我需要使用 switch 语句检查特定键是否存在。上面显示了带有附加属性的示例 JSON。
【问题讨论】:
标签: javascript json switch-statement