如何使用JavaScript從此字符串中讀取agent_code
?請給我解釋一下邏輯。如何從JavaScript中的JSON字符串讀取特定屬性?
JSON(單行):
[{"name":"NYC","zone_id":"1","totalagents":"40","agents":[{"id":"1","agent_code":"====="},{"id":"2","agent_code":"====="},{"id":"3","agent_code":"Christian"},{"id":"4","agent_code":"Tom"},{"id":"5","agent_code":"Dave Damsky"},{"id":"6","agent_code":"====="},{"id":"7","agent_code":"Andrew"},{"id":"8","agent_code":"Paolo"},{"id":"9","agent_code":"Josh"},{"id":"10","agent_code":"Shipster Van"},{"id":"11","agent_code":"====="},{"id":"16","agent_code":"Christian2"},{"id":"20","agent_code":"Nathan"},{"id":"21","agent_code":"Aaron"},{"id":"22","agent_code":"Rob"},{"id":"23","agent_code":"Taylor"},{"id":"24","agent_code":"Drea"},{"id":"25","agent_code":"Mario "},{"id":"26","agent_code":"Julio"},{"id":"27","agent_code":"Abbas"},{"id":"28","agent_code":"Ahmed"},{"id":"29","agent_code":"David Damsky"},{"id":"30","agent_code":"Micheal"},{"id":"31","agent_code":"Moe"},{"id":"32","agent_code":"Luis"},{"id":"33","agent_code":"Darin"},{"id":"37","agent_code":"Alan"},{"id":"39","agent_code":"Cristian Marte"},{"id":"40","agent_code":"Cody"},{"id":"41","agent_code":"David Pinto"},{"id":"42","agent_code":"Will "},{"id":"44","agent_code":"Evan"},{"id":"45","agent_code":"Santiago"},{"id":"46","agent_code":"John"},{"id":"47","agent_code":"Moubeen"},{"id":"49","agent_code":"Devin Armstrong"},{"id":"50","agent_code":"Marco Bell"},{"id":"51","agent_code":"Youness Benzaid"},{"id":"52","agent_code":"Amin Mechouche"},{"id":"53","agent_code":"Franco Herrera"}]}]
JSON(格式化):
[
{
"name": "NYC",
"zone_id": "1",
"totalagents": "40",
"agents": [
{
"id": "1",
"agent_code": "====="
},
{
"id": "2",
"agent_code": "====="
},
{
"id": "3",
"agent_code": "Christian"
},
{
"id": "4",
"agent_code": "Tom"
},
{
"id": "5",
"agent_code": "Dave Damsky"
},
{
"id": "6",
"agent_code": "====="
},
{
"id": "7",
"agent_code": "Andrew"
},
{
"id": "8",
"agent_code": "Paolo"
},
{
"id": "9",
"agent_code": "Josh"
},
{
"id": "10",
"agent_code": "Shipster Van"
},
{
"id": "11",
"agent_code": "====="
},
{
"id": "16",
"agent_code": "Christian2"
},
{
"id": "20",
"agent_code": "Nathan"
},
{
"id": "21",
"agent_code": "Aaron"
},
{
"id": "22",
"agent_code": "Rob"
},
{
"id": "23",
"agent_code": "Taylor"
},
{
"id": "24",
"agent_code": "Drea"
},
{
"id": "25",
"agent_code": "Mario "
},
{
"id": "26",
"agent_code": "Julio"
},
{
"id": "27",
"agent_code": "Abbas"
},
{
"id": "28",
"agent_code": "Ahmed"
},
{
"id": "29",
"agent_code": "David Damsky"
},
{
"id": "30",
"agent_code": "Micheal"
},
{
"id": "31",
"agent_code": "Moe"
},
{
"id": "32",
"agent_code": "Luis"
},
{
"id": "33",
"agent_code": "Darin"
},
{
"id": "37",
"agent_code": "Alan"
},
{
"id": "39",
"agent_code": "Cristian Marte"
},
{
"id": "40",
"agent_code": "Cody"
},
{
"id": "41",
"agent_code": "David Pinto"
},
{
"id": "42",
"agent_code": "Will "
},
{
"id": "44",
"agent_code": "Evan"
},
{
"id": "45",
"agent_code": "Santiago"
},
{
"id": "46",
"agent_code": "John"
},
{
"id": "47",
"agent_code": "Moubeen"
},
{
"id": "49",
"agent_code": "Devin Armstrong"
},
{
"id": "50",
"agent_code": "Marco Bell"
},
{
"id": "51",
"agent_code": "Youness Benzaid"
},
{
"id": "52",
"agent_code": "Amin Mechouche"
},
{
"id": "53",
"agent_code": "Franco Herrera"
}
]
}
]
將其解析爲Javascript對象。 – chRyNaN
當我解析它時返回錯誤「未捕獲的SyntaxError:意外的令牌o」 –
將其包裝在一個字符串中。單引號將是最簡單的。 –