[英]Javascript & Nested JSON issues
所以我正在嘗試將一些JSON放在一起並將其解析為jquery,但我的JSON語法出錯了。 我嘗試通過驗證器運行它,但它並沒有真正告訴我我做錯了什么。 有人可以指出我的方式的錯誤嗎?
var searchresults = [
{
"providerlisting": [
{
"nametitle": "Cory M Spears, MD, FACP",
"caretype": "Internal Medicine",
"preferredProvider": true,
"address1": "289 N. Highland Ave.",
"address2": "",
"cityStateZip": "Atlanta, GA 30306",
"coverage": "/images/example.png",
"status": "Out of Network",
"psn": "",
"dcontact": "urlhere",
"save": "urlhere",
"rating": "urlhere",
},
{
"nametitle": "Jimmy Dean, MD, FACP",
"caretype": "External Medicine",
"preferredProvider": false,
"address1": "3 Piedmont Rd.",
"address2": "",
"cityStateZip": "Atlanta, GA 30706",
"coverage": "/images/example2.png",
"status": "In Network",
"psn": "urlhere",
"dcontact": "urlhere",
"save": "urlhere",
"rating": "urlhere",
},
{
"nametitle": "John Doe, DD, PM",
"caretype": "Internal Medicine",
"preferredProvider": true,
"address1": "500 Ponce De Leon Ave",
"address2": "Suite 5",
"cityStateZip": "Atlanta, GA 30706",
"coverage": "/images/example2.png",
"status": "Out of Network",
"psn": "urlhere",
"dcontact": "urlhere",
"save": "urlhere",
"rating": "urlhere",
}]
},
{
"categories": [{
"categoryMenu": [
{
"providertype": [
{
"title": "Doctor",
"link": "#doctor",
"amount": "400"
},
{
"title": "Hospital",
"link": "#hospital",
"amount": "40"
},
{
"title": "Urgent Care",
"link": "#urgentCare",
"amount": "37"
}
]
},
{
"specialty": [
{
"title": "Allergy and Immunology",
"link": "#allergyAndImmunology",
"amount": "2"
},
{
"title": "Audiology",
"link": "#audiology",
"amount": "3"
},
{
"title": "Allergy and Immunology",
"link": "#allergyAndImmunology",
"amount": "6"
},
{
"title": "Ambulatory Surgical Center",
"link": "#ambulatorySurgicalCenter",
"amount": "4"
}
]
},
{
"gender": [
{
"title": "Male",
"link": "#male",
"amount": "67"
},
{
"title": "Female",
"link": "#female",
"amount": "3"
}
]
}
}]
}];
取下,
在每個端
"rating": "urlhere"
有一個]
缺少在倒數第三行,下面是一個有效的JSON對象
var searchresults = [{
"providerlisting": [{
"nametitle": "Cory M Spears, MD, FACP",
"caretype": "Internal Medicine",
"preferredProvider": true,
"address1": "289 N. Highland Ave.",
"address2": "",
"cityStateZip": "Atlanta, GA 30306",
"coverage": "/images/example.png",
"status": "Out of Network",
"psn": "",
"dcontact": "urlhere",
"save": "urlhere",
"rating": "urlhere"
}, {
"nametitle": "Jimmy Dean, MD, FACP",
"caretype": "External Medicine",
"preferredProvider": false,
"address1": "3 Piedmont Rd.",
"address2": "",
"cityStateZip": "Atlanta, GA 30706",
"coverage": "/images/example2.png",
"status": "In Network",
"psn": "urlhere",
"dcontact": "urlhere",
"save": "urlhere",
"rating": "urlhere"
}, {
"nametitle": "John Doe, DD, PM",
"caretype": "Internal Medicine",
"preferredProvider": true,
"address1": "500 Ponce De Leon Ave",
"address2": "Suite 5",
"cityStateZip": "Atlanta, GA 30706",
"coverage": "/images/example2.png",
"status": "Out of Network",
"psn": "urlhere",
"dcontact": "urlhere",
"save": "urlhere",
"rating": "urlhere"
}]
}, {
"categories": [{
"categoryMenu": [{
"providertype": [{
"title": "Doctor",
"link": "#doctor",
"amount": "400"
}, {
"title": "Hospital",
"link": "#hospital",
"amount": "40"
}, {
"title": "Urgent Care",
"link": "#urgentCare",
"amount": "37"
}]
}, {
"specialty": [{
"title": "Allergy and Immunology",
"link": "#allergyAndImmunology",
"amount": "2"
}, {
"title": "Audiology",
"link": "#audiology",
"amount": "3"
}, {
"title": "Allergy and Immunology",
"link": "#allergyAndImmunology",
"amount": "6"
}, {
"title": "Ambulatory Surgical Center",
"link": "#ambulatorySurgicalCenter",
"amount": "4"
}]
}, {
"gender": [{
"title": "Male",
"link": "#male",
"amount": "67"
}, {
"title": "Female",
"link": "#female",
"amount": "3"
}]
}]
}]
}];
只有,
在對象鍵/值對后逗號,如果有另外一個跟隨它。 (同樣適用於陣列)
例如:
var a = {
key : 'value',
keyB : 'value' // <-- there is no trailing comma before an object ends
};
你知道http://jsonlint.com/嗎?
你必須意識到,javascript中的對象幾乎與關聯數組相同。
我想你應該在Javascript中閱讀更多關於對象和數組的內容。
嘗試這個:
var searchresults = {
"providerlisting": [
{
"nametitle": "Cory M Spears, MD, FACP",
"caretype": "Internaenter code herel Medicine",
"preferredProvider": true,
"address1": "289 N. Highland Ave.",
"address2": "",
"cityStateZip": "Atlanta, GA 30306",
"coverage": "/images/example.png",
"status": "Out of Network",
"psn": "",
"dcontact": "urlhere",
"save": "urlhere",
"rating": "urlhere"
},
{
"nametitle": "Jimmy Dean, MD, FACP",
"caretype": "External Medicine",
"preferredProvider": false,
"address1": "3 Piedmont Rd.",
"address2": "",
"cityStateZip": "Atlanta, GA 30706",
"coverage": "/images/example2.png",
"status": "In Network",
"psn": "urlhere",
"dcontact": "urlhere",
"save": "urlhere",
"rating": "urlhere"
},
{
"nametitle": "John Doe, DD, PM",
"caretype": "Internal Medicine",
"preferredProvider": true,
"address1": "500 Ponce De Leon Ave",
"address2": "Suite 5",
"cityStateZip": "Atlanta, GA 30706",
"coverage": "/images/example2.png",
"status": "Out of Network",
"psn": "urlhere",
"dcontact": "urlhere",
"save": "urlhere",
"rating": "urlhere"
}
],
"categories": {
"categoryMenu": {
"providertype": [
{
"title": "Doctor",
"link": "#doctor",
"amount": "400"
},
{
"title": "Hospital",
"link": "#hospital",
"amount": "40"
},
{
"title": "Urgent Care",
"link": "#urgentCare",
"amount": "37"
}
],
"specialty": [
{
"title": "Allergy and Immunology",
"link": "#allergyAndImmunology",
"amount": "2"
},
{
"title": "Audiology",
"link": "#audiology",
"amount": "3"
},
{
"title": "Allergy and Immunology",
"link": "#allergyAndImmunology",
"amount": "6"
},
{
"title": "Ambulatory Surgical Center",
"link": "#ambulatorySurgicalCenter",
"amount": "4"
}
],
"gender": [
{
"title": "Male",
"link": "#male",
"amount": "67"
},
{
"title": "Female",
"link": "#female",
"amount": "3"
}
]
}
}
}
Please find the corrected Json string
var searchresults = [
{
"providerlisting": [
{
"nametitle": "Cory M Spears, MD, FACP",
"caretype": "Internal Medicine",
"preferredProvider": true,
"address1": "289 N. Highland Ave.",
"address2": "",
"cityStateZip": "Atlanta, GA 30306",
"coverage": "/images/example.png",
"status": "Out of Network",
"psn": "",
"dcontact": "urlhere",
"save": "urlhere",
"rating": "urlhere",
},
{
"nametitle": "Jimmy Dean, MD, FACP",
"caretype": "External Medicine",
"preferredProvider": false,
"address1": "3 Piedmont Rd.",
"address2": "",
"cityStateZip": "Atlanta, GA 30706",
"coverage": "/images/example2.png",
"status": "In Network",
"psn": "urlhere",
"dcontact": "urlhere",
"save": "urlhere",
"rating": "urlhere",
},
{
"nametitle": "John Doe, DD, PM",
"caretype": "Internal Medicine",
"preferredProvider": true,
"address1": "500 Ponce De Leon Ave",
"address2": "Suite 5",
"cityStateZip": "Atlanta, GA 30706",
"coverage": "/images/example2.png",
"status": "Out of Network",
"psn": "urlhere",
"dcontact": "urlhere",
"save": "urlhere",
"rating": "urlhere",
}]
},
{
"categories": [{
"categoryMenu": [
{
"providertype": [
{
"title": "Doctor",
"link": "#doctor",
"amount": "400"
},
{
"title": "Hospital",
"link": "#hospital",
"amount": "40"
},
{
"title": "Urgent Care",
"link": "#urgentCare",
"amount": "37"
}
]
},
{
"specialty": [
{
"title": "Allergy and Immunology",
"link": "#allergyAndImmunology",
"amount": "2"
},
{
"title": "Audiology",
"link": "#audiology",
"amount": "3"
},
{
"title": "Allergy and Immunology",
"link": "#allergyAndImmunology",
"amount": "6"
},
{
"title": "Ambulatory Surgical Center",
"link": "#ambulatorySurgicalCenter",
"amount": "4"
}
]
},
{
"gender": [
{
"title": "Male",
"link": "#male",
"amount": "67"
},
{
"title": "Female",
"link": "#female",
"amount": "3"
}
]
}
]
}]
}];
alert(JSON.stringify(searchresults))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.