向JSON对象添加一个新的数组元素

我有一个JSON格式的对象,我从一个名为teamJSON的variables中读取JSON文件,看起来像这样:

{"theTeam":[{"teamId":"1","status":"pending"},{"teamId":"2","status":"member"},{"teamId":"3","status":"member"}]} 

我想添加一个新的项目,如数组

 {"teamId":"4","status":"pending"} 

结束

 {"theTeam":[{"teamId":"1","status":"pending"},{"teamId":"2","status":"member"},{"teamId":"3","status":"member"},{"teamId":"4","status":"pending"}]} 

在回写到文件之前。 什么是添加到新元素的好方法? 我接近了,但所有的双引号都逃脱了。 我已经find了一个很好的答案,但没有涵盖这个案例。 任何帮助表示赞赏。

JSON只是一个符号 ; 要进行更改以parse它,以便您可以将更改应用到本机JavaScript对象 ,然后再将其stringify JSON

 var jsonStr = '{"theTeam":[{"teamId":"1","status":"pending"},{"teamId":"2","status":"member"},{"teamId":"3","status":"member"}]}'; var obj = JSON.parse(jsonStr); obj['theTeam'].push({"teamId":"4","status":"pending"}); jsonStr = JSON.stringify(obj); // "{"theTeam":[{"teamId":"1","status":"pending"},{"teamId":"2","status":"member"},{"teamId":"3","status":"member"},{"teamId":"4","status":"pending"}]}" 

如果你想添加在最后的位置,然后使用下面的代码

 var Str_txt = '{"theTeam":[{"teamId":"1","status":"pending"},{"teamId":"2","status":"member"},{"teamId":"3","status":"member"}]}'; var parse_obj = JSON.parse(Str_txt); parse_obj['theTeam'].push({"teamId":"4","status":"pending"}); Str_txt = JSON.stringify(parse_obj); Output //"{"theTeam":[{"teamId":"1","status":"pending"},{"teamId":"2","status":"member"},{"teamId":"3","status":"member"},{"teamId":"4","status":"pending"}]}" 

而且,如果你想添加在第一个位置,然后使用下面的代码

 var parse_obj = JSON.parse(Str_txt); parse_obj['theTeam'].unshift({"teamId":"4","status":"pending"}); Str_txt = JSON.stringify(parse_obj); Output //"{"theTeam":[{"teamId":"4","status":"pending"},{"teamId":"1","status":"pending"},{"teamId":"2","status":"member"},{"teamId":"3","status":"member"}]}" 

而且,任何人都想数组的某个位置添加,然后尝试下面的代码

 fruits['theTeam'].splice(2, 0, {"teamId":"4","status":"pending"}); Output //"{"theTeam":[{"teamId":"1","status":"pending"},{"teamId":"2","status":"member"},{"teamId":"4","status":"pending"},{"teamId":"3","status":"member"}]}" 

以上代码块在第二个元素之后添加一个元素。

例如,这里有一个像添加项目到篮子的button和适当的属性保存在localStorage中的元素。

 '<a href="#" cartBtn pr_id='+e.id+' pr_name_en="'+e.nameEn+'" pr_price="'+e.price+'" pr_image="'+e.image+'" class="btn btn-primary"><i class="fa fa-shopping-cart"></i>Add to cart</a>' 

 var productArray=[]; $(document).on('click','[cartBtn]',function(e){ e.preventDefault(); $(this).html('<i class="fa fa-check"></i>Added to cart'); console.log('Item added '); var productJSON={"id":$(this).attr('pr_id'), "nameEn":$(this).attr('pr_name_en'), "price":$(this).attr('pr_price'), "image":$(this).attr('pr_image')}; if(localStorage.getObj('product')!==null){ productArray=localStorage.getObj('product'); productArray.push(productJSON); localStorage.setObj('product', productArray); } else{ productArray.push(productJSON); localStorage.setObj('product', productArray); } }); Storage.prototype.setObj = function(key, value) { this.setItem(key, JSON.stringify(value)); } Storage.prototype.getObj = function(key) { var value = this.getItem(key); return value && JSON.parse(value); } 

添加JSON对象到Array结果(在LocalStorage中)之后:

[{“id”:“99”,“nameEn”:“Product Name1”,“price”:“767”,“image”:“1462012597217.jpeg”},{“id”:“93” :“产品名称2”,“价格”:“76”,“图像”:“1461449637106.jpeg”},{“id”:“94”,“nameEn”:“产品名称3”,“价格” , “图像”: “1461449679506.jpeg”}]

在这个动作之后,你可以很容易地将数据发送到服务器,如Java中的List

完整的代码示例在这里

如何使用localStorage存储简单的购物车?