简体   繁体   English

如何修改JSON数组?

[英]How to modify JSON array?

How can I update a value at a specific index in the following multi dimensional JSON array? 如何在以下多维JSON数组中的特定索引处更新值?

I would like to update the value of background-image placed inside the footer_logo node. 我想更新放置在footer_logo节点内的background-image的值。

{
    "Machine1": {
        "sidebar_inner": {
            "img": "img/pill.png",
            "background-color": "#ffffff",
            "side_logo": {
                "background-image": "../footer_logo.png"
            }
        },
        "lb_footer": {
            "img": "img/bin.png",
            "footer_logo": {
                "background-image": "..img/footer_logo.png"
            }
        },
        "machine_stand": {
            "img": "img/machine_stand.png"
        },
        "side": {
            "backgroundcolor": "#ccc"
        }
    }
}

I've added a jsfiddle example here . 我在这里添加了一个jsfiddle示例。 It's prety simple, take a look at the code below. 非常简单,请看下面的代码。

var myJson = {
        "Machine1": {
            "sidebar_inner": {
                "img": "img/pill.png",
                "background-color": "#ffffff",
                "side_logo": {
                    "background-image": "../footer_logo.png"
                }
            },
            "lb_footer": {
                "img": "img/bin.png",
                "footer_logo": {
                    "background-image": "..img/footer_logo.png"
                }
            },
            "machine_stand": {
                "img": "img/machine_stand.png"
            },
            "side": {
                "backgroundcolor": "#ccc"
            }
        }
    };

    myJson.Machine1.lb_footer.footer_logo['background-image'] = 'New value.';

    alert(myJson.Machine1.lb_footer.footer_logo['background-image']);

To update nested values, you would chain keys (indexes are for arrays) together to find the appropriate value. 要更新嵌套值,可以将键(索引用于数组)链接在一起以找到合适的值。

Here's the code, formatted for easy consumption: 这是代码,其格式易于使用:

var obj = {"Machine1": {
    "sidebar_inner": {
        "img": "img/pill.png",
        "background-color": "#ffffff",
        "side_logo": {
            "background-image": "../footer_logo.png"
        }
    },
    "lb_footer": {
        "img": "img/bin.png",
        "footer_logo": {
            "background-image": "..img/footer_logo.png"
        }
    },
    "machine_stand": { 
        "img": "img/machine_stand.png"
    }, 
    "side": {
        "backgroundcolor":"#ccc"
    }
}}

Examples 例子

To update "footer_logo"'s background image: obj.Machine1.lb_footer,footer_logo.background-image = "something new"; 更新“ footer_logo”的背景图片: obj.Machine1.lb_footer,footer_logo.background-image = "something new";

To update "machine_stand's" img: 更新“ machine_stand's” img:

obj.machine_stand.img = "new/link.jpg";
// or use 'bracket notation', it's the same thing
obj['machine_stand']['img'] = "new/link.jpg"

Try this. 尝试这个。 A working jsfiddle 一个工作的jsfiddle

    var json= '{"Machine1":{"sidebar_inner":{"img":"img\/pill.png","background-color":"#ffffff","side_logo":{"background-image":"..\/footer_logo.png"}},"lb_footer":{"img":"img\/bin.png","footer_logo":{"background-image":"..img\/footer_logo.png"}},"machine_stand":{"img":"img\/machine_stand.png"},"side":{"backgroundcolor":"#ccc"}}}';
    var jsonObj = JSON.parse(json);
    jsonObj.Machine1.lb_footer.footer_logo['background-image'] = 'abc';
arr["Machine1"]["lb_footer"]["footer_logo"]["background-image"] = mynewvalue;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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