简体   繁体   中英

How do you change the name of a root element in JSON with javascript?

Is it possible to change the variable name of this:

var grid = { 
    "name": "test", 
    "columns":
    [
        {"name":"col1"},
        {"name":"col2"}
    ], 
    "rows":
    [
         {"name":"col1", "value":"text1"},
         {"name":"col2", "value":"text2"}
    ] 
}

I want to change grid to grid1. Is this possible? Thanks for your guidance here. I did research this and did not find any solutions out there.

This isn't really JSON; it's just a JavaScript object. So "grid" is just a JavaScript variable. You can copy it to a variable with a different name, and delete the original, like this:

var grid1 = grid;
delete grid;

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