简体   繁体   中英

Reformat Json response using jQuery or javascript

I have a json object that I cannot control the feed of. The feed sends one object with an undifined array of objects with in it. I need each object to be defined at an asset to the asset with all its attribute (path, name, title, etc) with in each object with in assets like the second result below. If there is a functiond idea or a jquery pluggin that can help. Can some one help me get this result please.

Edit: I had to remove the json object for privacy reasons. Thank you to those that responded.

You can just iterate through the JSON array and rearrange the elements into an output array

var json = { "assets": [ ... ] };
var out = [];
for (var i in json.assets) {
    var v = json.assets[i];
    v.id = +i + 1;
    v.title_id = +i + 1;
    out.push({ 'asset': v });
}

JSFiddle for testing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM