javascript操纵json对象

Wil*_*Lou 0 javascript json

例如,我有以下JSON对象 json_obj1

json_obj1 = {x:{id:1,bars:{show:true,barWidth:0.4}}}
Run Code Online (Sandbox Code Playgroud)

现在,我怎么能添加以下对象(使用javascript):

   y:{id:2,bars:{show:true,barWidth:0.4}}
Run Code Online (Sandbox Code Playgroud)

json_obj1这样,这将是:

{x:{id:1,bars:{show:true,barWidth:0.4}},y:{id:2,bars:{show:true,barWidth:0.4}}}
Run Code Online (Sandbox Code Playgroud)

goa*_*nks 6

您只需设置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}}
Run Code Online (Sandbox Code Playgroud)

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