替换JSON中的属性值 [英] Replacing a property value in JSON

查看:63
本文介绍了替换JSON中的属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的JSON结构看起来像这样:

If I have a JSON structure that looks something like this:

var user = {
    map: {
        width: 785,
        height: 791
    },
    image: {
        name: "image.png",
        size: {width:32}
    },
    properties:[{
        firstName: "Bob",
        lastName: "Jones",
    }]
};

如何(在创建后)将firstName属性的值更改为简"?

How would I change (after creation) the value of the firstName property to "Jane"?

我对JSON还是很陌生,我只是想弄清楚现在如何进行这一更改.任何帮助将不胜感激.

I am fairly new to JSON, and I'm just trying to figure out how to make this one change for now. Any help would be greatly appreciated.

推荐答案

好吧,您感到困惑的原因之一可能是它根本不是JSON. JSON是用于序列化对象的文本格式.这只是Javascript中的文字对象.

Well, one reason for your confusion might be that this is not JSON at all. JSON is a text format used for serialising objects. This is just a literal object in Javascript.

要更改firstName属性,您可以访问user对象中properties数组中的第一项:

To change the firstName property, you would access the first item in the properties array in the user object:

user.properties[0].firstName = "Jane";

这篇关于替换JSON中的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆