重命名对象中的字段 [英] Renaming a field in an 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 }
}
}
};
此JSON对象被解析为变量 parsedJSON
使用此JavaScript代码:
This JSON object is parsed into the variable parsedJSON
using this JavaScript code:
var parsedJSON = JSON.parse(JsonObj);
如何重命名 parsedJSON中的frames属性
到别的什么地方?
How would I rename the "frames" property in parsedJSON
to something else?
推荐答案
将 somethingElse
设置为参考什么 frames
指向,然后删除 frames
。
Set the somethingElse
as a reference to what frames
points to, then delete frames
.
parsedJSON.somethingElse = parsedJSON.frames;
delete parsedJSON.frames;
这里重要的是 frames
是只是一个指向对象的指针;如果删除 frames
指针, somethingElse
仍引用有效对象。
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.
还要注意,没有JSON对象这样的东西;你有一个对象的JSON表示,这是一个字符串,或你有一个对象(通常可以通过对象文字符号来定义,这通常是混淆所在的地方)。
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).
这篇关于重命名对象中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!