Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从JavaScript中的JSON字符串读取特定属性?_Javascript_Json - Fatal编程技术网

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

如何从JavaScript中的JSON字符串读取特定属性?,javascript,json,Javascript,Json,如何使用JavaScript从该字符串中读取agent\u code?请给我解释一下逻辑 JSON(一行): “40”、“代理”:““{”姓名姓名姓名姓名姓名:“{{”姓名姓名姓名姓名姓名:“{”姓名姓名姓名姓名姓名姓名姓名:“{”姓名姓名姓名姓名姓名姓名姓名姓名姓名:“纽约市纽约市纽约市纽约市纽约市纽约市纽约市纽约市纽约市纽约市纽约市纽约市纽约市纽约市纽约市、地带和分区的id id id id id id号id::“1”40”、“代理:“,,代理:“门门门门门门门门门门门门门门第:::::““

如何使用JavaScript从该字符串中读取
agent\u code
?请给我解释一下逻辑

JSON(一行)

“40”、“代理”:““{”姓名姓名姓名姓名姓名:“{{”姓名姓名姓名姓名姓名:“{”姓名姓名姓名姓名姓名姓名姓名:“{”姓名姓名姓名姓名姓名姓名姓名姓名姓名:“纽约市纽约市纽约市纽约市纽约市纽约市纽约市纽约市纽约市纽约市纽约市纽约市纽约市纽约市纽约市、地带和分区的id id id id id id号id::“1”40”、“代理:“,,代理:“门门门门门门门门门门门门门门第:::::““““““{{{门门姓名姓名姓名姓名姓名姓名姓名姓名姓名姓名姓名姓名姓名::::::”,,,,门门门门门门门门门门门门门门门门门门门门门门门门门门门门门门门门门门门门第:“““““““““““““““““““““““““““:::::::::::::“Josh”{“id”:“10”,“代理人代码”:“Shipster Van”},{“id”:“11”,“代理人代码”:“==”,{“id”:“16”,“代理人代码”:“Christian 2”},{“id”:“20”,“代理人代码”:“Nathan”},{“id”:“21”,“代理人代码”:“Aaron”{“id”:“22”,“代理人代码”:“Rob”{“id”:“23”,“代理人代码”:“Taylor”{“id”:“24”,“代理人代码”:“Nathan”{“id”:“Drea 21”,“代理人代码”:“Aaron”{“Mario”{“代理人代码”:“Mario,{“id”:“27”,“代理代码”:“Abbas”},{“id”:“28”,“代理代码”:“Ahmed”},{“id”:“29”,“代理代码”:“David Damsky”},{“id”:“30”,“代理代码”:“Micheal”},{“id”:“31”,“代理代码”:“Moe”},{“id”:“32”,“代理代码”:“Luis”},{“id”:“33”,“代理代码”:“Darin”},{“id”:“37”,“代理代码”:“Alan”},{“Alan”},{“id”:“39”,“Cristian”},{“代理代码”:“Cody”{“代理代码”:“40”{“id”:“:”41“,”代理代码“:”大卫·平托“,”42“,”代理代码“:”威尔“,”id“,”44“,”代理代码“:”埃文“,”id“:”45“,”代理代码“:”圣地亚哥“,”id“:”46“,”代理代码“:”约翰“,”id“:”47“,”代理代码“:”穆贝“,”id“,”49“,”代理代码“,”德文·阿姆斯特朗“,”id“,”50“,”代理代码“:”马尔科“,”本泽德“,”贝尔“,”代理代码“,”52“,”:“Amin Mechouche”},{“id”:“53”,“代理人代码”:“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"
            }
        ]
    }
]

假设您的上面是一个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);

}

for(var i=0;i假设您的上面是一个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);

}

for(var i=0;我将其解析为Javascript对象。当我将其解析为“Uncaught SyntaxError:Unexpected token o”时,它将返回错误,并将其包装为字符串。单引号最简单。将其解析为Javascript对象。当我将其解析为“Uncaught SyntaxError:Unexpected token o”时,它将返回错误将其包装在字符串中。单引号最简单。将其解析为Javascript对象。当我将其解析为“Uncaught SyntaxError:Unexpected token o”时,它返回错误。将其包装在字符串中。单引号最简单。