這是我的第一個問題StackOverflow。我認爲答案並不那麼複雜,但我很新Javascript。如何將JSON轉換爲陣列
我有一個JQuery的AJAX函數解析這個JSON對象:
{
"Users":[
{
"key":"1",
"label":"Tom Clancy"
},
{
"key":"12",
"label":"Steve Martin"
}
]
}
,並應獲得相同的結果:
var sections = [{
key: 1,
label: "Tom Clancy"
}, {
key: 12,
label: "Steve Martin"
}
];
我能夠通過迭代JSON元素,但我不知道如何繼續。
您能提供建議嗎?
編輯:我仍然不能得到它的工作......這是我的代碼:
var sections=[
{key:1, label:"Section A"},
{key:2, label:"Section B"},
{key:3, label:"Section C"},
{key:4, label:"Section D"}
];
$.ajax({
url: '/WebOffice/ListaUtenti',
type: "POST",
dataType: 'json',
success: function (data)
{
console.log("success");
sections = data.Users;
}});
scheduler.createTimelineView({
name: "matrix",
x_unit: "day",
x_date: "%d %M",
x_step: 1,
x_size: 15,
y_unit: sections,
y_property: "section_id"
});
jQuery的AJAX調用不分配新值的部分(呼叫狀態是成功,驗證),所以調度程序仍然顯示原始部分。我錯在哪裏?
謝謝
'var sections = json_object.Users'? – Dogbert 2013-04-06 19:27:08
若要將其轉換爲數組,您可以執行'var arr = $ .makeArray(object)',但實際上沒有理由,因爲您可以輕鬆訪問該對象。 – adeneo 2013-04-06 19:28:30
這麼簡單?我真的很新的JavaScript ...非常感謝你! – LucaCDO 2013-04-06 19:30:27