JSON到可用的数组javascript

时间:2012-08-14 12:39:50

标签: json

我试图将我的JSON代码转换为javascript / jquery中的可用数组。

我有以下通过ajax到达的JSON代码:

[{"id":"9","firstname":"Greg","surname":"Bril","position":"0","busy":"0","disabled":"0"},{"id":"14","firstname":"Nai","surname":"Brooks","position":"1","busy":"0","disabled":"0"},{"id":"17","firstname":"Margaret","surname":"Grey","position":"1","busy":"0","disabled":"0"},{"id":"1","firstname":"Cameron","surname":"Grover","position":"0","busy":"0","disabled":"0"},{"id":"2","firstname":"Sarah","surname":"Grover","position":"0","busy":"0","disabled":"0"},{"id":"3","firstname":"Margaret","surname":"Hynes","position":"0","busy":"0","disabled":"0"},{"id":"4","firstname":"Stephen","surname":"Hynes","position":"0","busy":"0","disabled":"0"},{"id":"11","firstname":"Ben","surname":"Mills","position":"1","busy":"0","disabled":"0"},{"id":"15","firstname":"Elizabeth","surname":"Mills","position":"1","busy":"0","disabled":"0"},{"id":"10","firstname":"Grant","surname":"Mills","position":"0","busy":"0","disabled":"0"},{"id":"16","firstname":"John","surname":"Mills","position":"1","busy":"0","disabled":"0"},{"id":"13","firstname":"Lucinda","surname":"Ower","position":"1","busy":"0","disabled":"0"},{"id":"12","firstname":"Karina","surname":"Scott","position":"1","busy":"0","disabled":"0"}]

使用以下方法创建和解释:

$.getJSON( "tc_search1.php", {
        leave: $("input#leave").val(),
        end: $("input#end").val(),
        override: $("#tc_override").is(":checked"),
        tc_id: $("#tc_id").val()
    }, function(data) {
         //i cant get this part to work
    });

我似乎无法获得成功的功能。我在jquery文档网站上尝试了$ .each方法,但我做不到。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

getJSON方法会自动将JSON字符串解析为Javascript对象。

成功回调函数中的data参数将是一个对象数组。例如,表达式data[0].firstname将返回"Greg"