这是我的第一个问题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