【发布时间】:2016-03-11 11:21:00
【问题描述】:
问题
我有以下未格式化的示例 json 数据:
"stations": {
"st1": "station 1",
"st2": "station 2",
"st3": "Station 3",
}
问题
如何将数据重新格式化为:
"stations": [
{
"id": "st1",
"name": "station 1",
},
{
"id": "st2",
"name": "station 2",
},
{
"id": "st3",
"name": "station 3",
}
]
试过
我试图简单地记录数据以首先进行测试,但我正在努力如何在键/值对之间进行实际迭代,因为它们本质上是字符串
这是我尝试过的:
$.get( '/js/ajax/tube-data.json', function( data ) {
$.each(data.stations, function () {
// I was expecting st1, st2, st3 to show in the console
// but got first letter of each station
console.log(this[0])
});
}).error(function() {console.log(arguments) });
有没有更好的方法来做到这一点?
【问题讨论】:
标签: javascript jquery json