开发者

what's the quick and easy way to replace a key in a json response with some text instead of a numerical key?

I have a json response that is somewhat unusable.

Here is what it looks like:


    {
        "9071": {
        "id": "9071",
        "name": "Front Row Side",
        "color": "",
        "sections": [
            {
            "id": "47447",
            "name": "Front Row Side 134",
            "coords": []
            },
            {
            "id": "47443",
            "name": "Front Row Side 120",
            "coords": []
            },
            {
            "id": "47442",
            "name": "Front Row Side 119",
            "coords": []
            },
            {
            "id": "47445",
            "name": "Front Row Side 132",
            "coords": []
            },
            {
            "id": "47444",
            "name": "Front Row Side 121",
            "coords": []
            },
            {
            "id": "47439",
            "name": "Front Row Side 115",
            "coords": []
            },
            {
            "id": "47438",
            "name": "Front Row Side 104",
            "coords": []
            },
            {
            "id": "47441",
            "name": "Front Row Side 117",
            "coords": []
            },
            {
            "id": "47440",
            "name": "Front Row Side 116",
            "coords": []
            },
            {
            "id": "47446",
            "name": "Front Row Side 133",
            "coords": []
            },
            {
            "id": "47437",
            "name": "Front Row Side 103",
            "coords": []
            },
            {
            "id": "47436",
            "name": "Front Row Side 102",
            "coords": []
            },
            {
            "id": "227801",
            "name": "Front Row Side 131",
            "coords": []
            }
        ]
        },
        "9072": {
        "id": "9072",
        "name": "Front Row End Zone",
        "color": "",
        "sections": [
            {
            "id": "47453",
            "name": "Front Row End Zone 114",
            "coords": []
            },
            {
            "id": "47454",
            "name": "Front Row End Zone 124",
            "coords": []
            },
            {
            "id": "47455",
            "name": "Front Row End Zone 125",
            "coords": []
            },
            {
            "id": "47456",
            "name": "Front Row End Zone 128",
            "coords": []
            },
            {
            "id": "47449",
            "name": "Front Row End Zone 107",
            "coords": []
            },
            {
            "id": "47450",
            "name": "Front Row End Zone 108",
            "coords": []
            },
            {
            "id": "47451",
            "name": "Front Row End Zone 111",
            "coords": []
            },
            {
            "id": "47452",
            "name": "Front Row End Zone 112",
            "coords": []
            },
            {
            "id": "47457",
            "name": "Front Row End Zone 129",
            "coords": []
            },
            {
            "id": "47448",
            "name": "Front Row End Zone 105",
            "coords": []
            }
        ]
        },
        "9073": {
        "id": "9073",
        "name": "Panthers Club Side开发者_Go百科",
        "color": "",
        "sections": [
            {
            "id": "47459",
            "name": "Panthers Club Side 102",
            "coords": []
            },
            {
            "id": "47458",
            "name": "Panthers Club Side 101",
            "coords": []
            },
            {
            "id": "47471",
            "name": "Panthers Club Side 132",
            "coords": []
            },
            {
            "id": "47472",
            "name": "Panthers Club Side 133",
            "coords": []
            },
            {
            "id": "47473",
            "name": "Panthers Club Side 134",
            "coords": []
            },
            {
            "id": "47462",
            "name": "Panthers Club Side 115",
            "coords": []
            },
            {
            "id": "47463",
            "name": "Panthers Club Side 116",
            "coords": []
            },
            {
            "id": "47460",
            "name": "Panthers Club Side 103",
            "coords": []
            },
            {
            "id": "47461",
            "name": "Panthers Club Side 104",
            "coords": []
            },
            {
            "id": "47466",
            "name": "Panthers Club Side 119",
            "coords": []
            },
            {
            "id": "47464",
            "name": "Panthers Club Side 117",
            "coords": []
            },
            {
            "id": "47465",
            "name": "Panthers Club Side 118",
            "coords": []
            },
            {
            "id": "47470",
            "name": "Panthers Club Side 131",
            "coords": []
            },
            {
            "id": "47469",
            "name": "Panthers Club Side 122",
            "coords": []
            },
            {
            "id": "47468",
            "name": "Panthers Club Side 121",
            "coords": []
            },
            {
            "id": "47467",
            "name": "Panthers Club Side 120",
            "coords": []
            }
        ]
        },


Every place where the set of array beings with a number - like this: "9072": { or "9073": { , i basically need to just remove that.

Any ideas?


It sounds like you want to convert the Object to an Array and have each direct property of the main object be an index of the array. If so, you can do this like:

var o = JSON.parse(yourData),
    a = [];

for (var p in o) {
    if (o.hasOwnProperty(p)) {
        a.push(o[p]);
    }
}

Then the array a will contain the objects contained in the main properties such that a[0] == o['9071'], a[1] == o['9072'], and a[2] == o['9073'].

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜