javascript 操作 json 对象

标签 javascript json

例如,我有以下 JSON 对象 json_obj1

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

现在,我如何添加以下对象(使用 JavaScript):

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

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,bars:{show:true,barWidth:0.4}},y:{id:2,bars:{show:true,barWidth:0.4}}}

关于javascript 操作 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1943723/

相关文章:

javascript - 带有 m3u8 url 的 Chromecast SDK

json - Elasticsearch 2.1 查询仅从数组中获取一个元素

javascript - 如何用不同的字符替换出现的相同字符

javascript - 从第一个数组中删除重复的索引值,按照第一个操作第二个(在某些特定条件下)

javascript - 如何检查 jQuery 查找返回 true 还是 false?

java - JsonMappingException 重复属性... [简单类型,类 java.lang.Exception]

android - 有谁知道如何在React Native中从json中获取数据?

Javascript 关系运算符显示字符串的奇怪行为

javascript - JQuery的Ajax是否引用web.config中的maxJsonLength属性

javascript - 如何使用 AngularJS 过滤对象属性