[英]how to add key and a value to a json using javascript or jquery
i have a json variable like this 我有一个像这样的json变量
var jsondata={"all":"true"}
i want to push another key and value to the jsondata. 我想将另一个键和值推送到jsondata。 after that my jsondata have to be like this.
之后我的jsondata必须像这样。
{"all":"true","FDamount":"false","DDamount":"true"}
how to do that??? 怎么做??? i tried jsondata.push({"FDamount":"false"}) and jsondata.push("FDamount:false").
我试过jsondata.push({“FDamount”:“false”})和jsondata.push(“FDamount:false”)。 both of these method is not working.
这两种方法都不起作用。
thank you 谢谢
Like this 像这样
jsondata.FDamount = 'false';
// or
jsondata['FDamount'] = 'false';
Simply do this : 只需这样做:
jsondata['FDamount'] = 'false';
jsondata['DDamount'] = 'true';
Or this : 或这个 :
jsondata.FDamount = 'false';
jsondata.DDamount = 'true';
By the way, you define boolean as string, the correct way should be : 顺便说一句,你将boolean定义为string,正确的方法应该是:
jsondata['FDamount'] = false;
jsondata['DDamount'] = true;
To push a little bit further, you can use jQuery.extend to extend the original var, like this : 为了进一步推进,您可以使用jQuery.extend扩展原始var,如下所示:
jQuery.extend(jsondata, {'FDamount': 'false', 'DDamount': 'true'});
// Now, jsondata will be :
{"all":"true","FDamount":"false","DDamount":"true"}
jQuery.extend is available when using jQuery (of course), but I'm sure you can find similar methods in other libraries/frameworks. 当使用jQuery(当然)时, jQuery.extend是可用的,但我相信你可以在其他库/框架中找到类似的方法。
(I'm using single quotes, but double quotes works too) (我使用单引号,但双引号也有效)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.