javascript操作json对象

javascript操作json对象,javascript,json,Javascript,Json,例如,我有以下JSON对象JSON\u obj1 json_obj1 = {x:{id:1,bars:{show:true,barWidth:0.4}}} 现在,我如何添加以下对象(使用javascript): 至json_obj1,以便: {x:{id:1,bars:{show:true,barWidth:0.4}},y:{id:2,bars:{show:true,barWidth:0.4}}} 您只需设置json_obj1的字段y json_obj1 = {x:{id:1,bars:{s

例如,我有以下JSON对象
JSON\u obj1

json_obj1 = {x:{id:1,bars:{show:true,barWidth:0.4}}}
现在,我如何添加以下对象(使用javascript):

json_obj1
,以便:

{x:{id:1,bars:{show:true,barWidth:0.4}},y:{id:2,bars:{show:true,barWidth:0.4}}}

您只需设置json_obj1的字段y

json_obj1 = {x:{id:1,bars:{show:true,barWidth:0.4}}}
json_obj1.y = {id:2,bars:{show:true,barWidth:0.4}}

现在json_obj1={x:{id:1,条:{show:true,barWidth:0.4}},y:{id:2,条:{show:true,barWidth:0.4}}}

看起来你的问题实际上并没有涉及json。第一个代码片段只是一个JavaScript对象文本。根据您对问题的描述,类似这样的方法应该可以奏效:

json_obj1 = {x:{id:1,bars:{show:true,barWidth:0.4}}};
json_obj1.y = {id:2,bars:{show:true,barWidth:0.4}};
这将为您提供json_obj1中所需的内容

json_obj1 = {x:{id:1,bars:{show:true,barWidth:0.4}}};
json_obj1.y = {id:2,bars:{show:true,barWidth:0.4}};