如何删除JSON标头?

时间:2012-11-12 09:57:05

标签: javascript json node.js

我有以下JSON:

 {
"folder": [
    {
        "$": {
            "id": "471841542",
            "name": "Manajemen Pemasaran",
            "description": "",
            "user_id": "186868958",
            "shared": "1",
            "shared_link": "https://www.box.com/s/b0nupgg8boe25nyfz781",
            "permissions": "kcfgtedopnhsuvy",
            "size": "2258167",
            "file_count": "5",
            "created": "1352203782",
            "updated": "1352260971"
        },
        "tags": [
            {}
        ]
    },
    {
        "$": {
            "id": "469509910",
            "name": "C++",
            "description": "",
            "user_id": "186868958",
            "shared": "1",
            "shared_link": "https://www.box.com/s/byyidt02k4sglbuvxlw3",
            "permissions": "kcfgtedopnhsuvy",
            "size": "1322331",
            "file_count": "128",
            "created": "1352101667",
            "updated": "1352203670"
        },
        "tags": [
            {}
        ]
    },
    {
        "$": {
            "id": "469476618",
            "name": "Default Sync Folder",
            "description": "",
            "user_id": "186868958",
            "shared": "0",
            "shared_link": "",
            "permissions": "kcfgtedopnhsuvy",
            "size": "49509",
            "file_count": "1",
            "created": "1352098703",
            "updated": "1352098713"
        },
        "tags": [
            {}
        ]
    },
    {
        "$": {
            "id": "469469972",
            "name": "My Box Files",
            "description": "",
            "user_id": "186868958",
            "shared": "0",
            "shared_link": "",
            "permissions": "kcfgtedopnhsuvy",
            "size": "32426",
            "file_count": "1",
            "created": "1352097877",
            "updated": "1352097977"
        },
        "tags": [
            {}
        ]
    }
 ]
 }

我希望它改变成这样:

 {
"folder": [{
            "id": "471841542",
            "name": "Manajemen Pemasaran",
            "description": "",
            "user_id": "186868958",
            "shared": "1",
            "shared_link": "https://www.box.com/s/b0nupgg8boe25nyfz781",
            "permissions": "kcfgtedopnhsuvy",
            "size": "2258167",
            "file_count": "5",
            "created": "1352203782",
            "updated": "1352260971"
                "tags": [
            {}
        ]
    },
    {
            "id": "469509910",
            "name": "C++",
            "description": "",
            "user_id": "186868958",
            "shared": "1",
            "shared_link": "https://www.box.com/s/byyidt02k4sglbuvxlw3",
            "permissions": "kcfgtedopnhsuvy",
            "size": "1322331",
            "file_count": "128",
            "created": "1352101667",
            "updated": "1352203670",
                "tags": [
            {}
        ]
    },
    {
            "id": "469476618",
            "name": "Default Sync Folder",
            "description": "",
            "user_id": "186868958",
            "shared": "0",
            "shared_link": "",
            "permissions": "kcfgtedopnhsuvy",
            "size": "49509",
            "file_count": "1",
            "created": "1352098703",
            "updated": "1352098713",
            "tags": [
            {}
        ]
    },
    {
        "$": {
            "id": "469469972",
            "name": "My Box Files",
            "description": "",
            "user_id": "186868958",
            "shared": "0",
            "shared_link": "",
            "permissions": "kcfgtedopnhsuvy",
            "size": "32426",
            "file_count": "1",
            "created": "1352097877",
            "updated": "1352097977"
                "tags": [
            {}
        ]
    }
 ]
 }

有没有办法从上面的JSON中删除$?

1 个答案:

答案 0 :(得分:1)

这应该创建你想要的结果:

var json = { ... };    // your JSON data
var newJson = { folder: [] };

for (var i = 0, l = json.folder.length; i < l; ++i) {
    var tmp = json.folder[i].$;
    tmp.tags = json.folder[i].tags;
    newJson.folder.push(tmp);
}

console.log(newJson);