i have a json variable like this
我有一个像这样的json变量
var jsondata={"all":"true"}
i want to push another key and value to the jsondata. after that my jsondata have to be like this.
我想将另一个键和值推送到jsondata。之后我的jsondata必须像这样。
{"all":"true","FDamount":"false","DDamount":"true"}
how to do that??? i tried jsondata.push({"FDamount":"false"}) and jsondata.push("FDamount:false"). both of these method is not working.
怎么做???我试过jsondata.push({“FDamount”:“false”})和jsondata.push(“FDamount:false”)。这两种方法都不起作用。
thank you
谢谢
2 个解决方案
#1
21
Like this
喜欢这个
jsondata.FDamount = 'false';
// or
jsondata['FDamount'] = 'false';
#2
8
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)
(我使用单引号,但双引号也有效)
#1
21
Like this
喜欢这个
jsondata.FDamount = 'false';
// or
jsondata['FDamount'] = 'false';
#2
8
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)
(我使用单引号,但双引号也有效)