如何从JavaScript中的JSON字符串中读取特定属性?

时间:2015-06-11 17:05:30

标签: javascript json

如何使用JavaScript从此字符串中读取agent_code?请解释我的逻辑。

JSON(单行)

  

[{"名称":" NYC"" zone_id":" 1"" totalagents&#34 ;: " 40""试剂":[{" ID":" 1"" agent_code":&#34 ; ====="},{" ID":" 2"" agent_code":"&===== #34;},{" ID":" 3"" agent_code":"基督教"},{" ID&# 34;:" 4"" agent_code":"汤姆"},{" ID":" 5&#34 ;, " agent_code":" Dave Damsky"},{" id":" 6"," agent_code":&# 34; ====="},{" ID":" 7"" agent_code":"安德鲁"} ,{" ID":" 8"" agent_code":"保罗"},{" ID":& #34; 9"" agent_code":"约什"},{" ID":" 10"" agent_code":" Shipster Van"},{" id":" 11"," agent_code":" == ==="},{" ID":" 16"" agent_code":" Christian2"},{&# 34; ID":" 20"" agent_code":"内森"},{" ID":" 21& #34; " agent_code":"亚伦"},{" ID":" 22"" agent_code":&# 34;罗布"},{" ID":" 23"" agent_code":"泰勒"},{&#34 ; ID":" 24"" agent_code":" Drea"},{" ID":" 25&# 34;," agent_code":" Mario"},{" id":" 26"," agent_code" :"胡"},{" ID":" 27"" agent_code":"阿巴斯"},{ " ID":" 28"" agent_code":"艾哈迈德"},{" ID":&#34 ; 29"," agent_code":" David Damsky"},{" id":" 30"," agent_code& #34;:"迈克尔"},{" ID":" 31"" agent_code":"萌" },{" ID":" 32"" agent_code":"路易斯"},{" ID&#34 ;: #&34; 33"" agent_code":"达林"},{" ID":" 37"&#34 ; agent_code":" Alan"},{" id":" 39"," agent_code":" Cristian Marte& #34;},{" ID":" 40"" agent_代码":" Cody"},{" id":" 41"," agent_code":" David Pinto&# 34;},{" id":" 42"," agent_code":"将"},{" id&# 34;:" 44"" agent_code":"埃文"},{" ID":" 45&#34 ;, " agent_code":"圣地亚哥"},{" ID":" 46"" agent_code":&#34 ;约翰"},{" ID":" 47"" agent_code":" Moubeen"},{" id":" 49"," agent_code":" Devin Armstrong"},{" id":" 50&# 34;," 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"
            }
        ]
    }
]

1 个答案:

答案 0 :(得分:0)

让我们说你的上面是一个json字符串

var jsonString  = '[{"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"}]}]';

var json = JSON.parse(jsonString); // parse string into json

现在这个json是一个长度为1的数组,要获取所有代理代码,你必须做这样的事情。

for (var i = 0; i <json[0].agents.length; i++) {
      console.log(json[0].agents[i].agent_code);

}