我收到一个json
字符串,如下所示:
[{"id":1,"class_code":1,"first_name":"Barry","last_name":"Allen","grade":null,"behavior":1,"beh_comments":null,"random_seating":null,"ascending_seats":null,"descending_seats":null},{"id":2,"class_code":1,"first_name":"Bruce","last_name":"Banner","grade":null,"behavior":2,"beh_comments":null,"random_seating":null,"ascending_seats":null,"descending_seats":null}
我需要将其转换为javascript
数组。这段代码:
var arr = Object.keys(jsonRoster).map(function (key) {return jsonRoster[key]});
给了我每个角色("b","a","r","r","y")
,但我想访问整个“barry”。
答案 0 :(得分:0)
您可以使用JSON.parse方法获取数组,然后根据需要访问名字或姓氏,如果这是您要执行的操作。
var j = '[{"id":1,"class_code":1,"first_name":"Barry","last_name":"Allen","grade":null,"behavior":1,"beh_comments":null,"random_seating":null,"ascending_seats":null,"descending_seats":null},{"id":2,"class_code":1,"first_name":"Bruce","last_name":"Banner","grade":null,"behavior":2,"beh_comments":null,"random_seating":null,"ascending_seats":null,"descending_seats":null}]';
var arr = JSON.parse(j)
然后你可以使用类似的东西:
arr[0].first_name
如果您正在寻找,请告诉我。