如何从URL重新格式化我的数据响应

时间:2012-06-29 20:18:10

标签: javascript jquery json

我从网址获取的数据格式采用以下格式:

#RES#[{"status":"Stopped \/ Idle","val":17.469444444444,"unit":"617-0003"},{"status":"Working","val":0,"unit":"617-0003"},{"status":"Headland Turning","val":0,"unit":"617-0003"},{"status":"Transport","val":0.15333333333333,"unit":"617-0003"}]

我想将其重新格式化为:

[
    {
    "status": "Stopped \/ Idle",
    "val": 17.469444444444},
{
    "status": "Working",
    "val": 0},
{
    "status": "Headland Turning",
    "val": 0},
{
    "status": "Transport",
    "val": 0.15333333333333}
]

注意:

  1. 删除开头字符串#RES#
  2. 删除字段"unit"
  3. 感谢。

1 个答案:

答案 0 :(得分:0)

var str = '#RES#[{"status":"Stopped \/ Idle","val":17.469444444444,"unit":"617-0003"},{"status":"Working","val":0,"unit":"617-0003"},{"status":"Headland Turning","val":0,"unit":"617-0003"},{"status":"Transport","val":0.15333333333333,"unit":"617-0003"}]';
str = str.slice(5);

var json = JSON.parse(str);
console.log(json);
for (var i = 0; i < json.length; i++) {
    delete json[i].unit;
}

console.log(json);