最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Renaming a field in an object - Stack Overflow

programmeradmin0浏览0评论

If I have the following object:

JsonObj = {
    "frames": {
        "cinema": {
            "sourceSize": { "w": 256, "h": 200 },
            "frame": { "x": 0, "y": 0, "w": 256, "h": 192 }
        },
        "tree": {
            "sourceSize": { "w": 128, "h": 110 },
            "frame": { "x": 0, "y": 302, "w": 70, "h": 96 }
        }
    }
};

This JSON object is parsed into the variable parsedJSON using this JavaScript code:

var parsedJSON = JSON.parse(JsonObj);

How would I rename the "frames" property in parsedJSON to something else?

If I have the following object:

JsonObj = {
    "frames": {
        "cinema": {
            "sourceSize": { "w": 256, "h": 200 },
            "frame": { "x": 0, "y": 0, "w": 256, "h": 192 }
        },
        "tree": {
            "sourceSize": { "w": 128, "h": 110 },
            "frame": { "x": 0, "y": 302, "w": 70, "h": 96 }
        }
    }
};

This JSON object is parsed into the variable parsedJSON using this JavaScript code:

var parsedJSON = JSON.parse(JsonObj);

How would I rename the "frames" property in parsedJSON to something else?

Share Improve this question edited Nov 25, 2013 at 15:58 Felix Kling 817k180 gold badges1.1k silver badges1.2k bronze badges asked Feb 9, 2013 at 15:05 Jamie FearonJamie Fearon 2,63413 gold badges49 silver badges65 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 19

Set the somethingElse as a reference to what frames points to, then delete frames.

parsedJSON.somethingElse = parsedJSON.frames;
delete parsedJSON.frames;

The important thing here is that frames is simply a pointer to an object; if you delete the frames pointer, somethingElse still references a valid object.


Also note there's no such thing as a "JSON object"; you have a JSON representation of an object, which is a string, or you have an object (which can often be defined via object literal notation, which is often where the confusion lies).

发布评论

评论列表(0)

  1. 暂无评论