我有一个JSON格式对象,我从一个名为teamJSON的变量中的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"}]}
在写回文件之前。添加到新元素的好方法是什么?我接近但所有双引号都被逃脱了。我已经找到了一个很好的答案,但没有一个完全覆盖这个案例。任何帮助表示赞赏。
答案 0 :(得分:186)
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"}]}"
答案 1 :(得分:18)
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"}]}"
上面的代码块在第二个元素之后添加一个元素。
答案 2 :(得分:1)
首先我们需要解析JSON对象,然后我们可以添加一个项目。
var str = '{"theTeam":[{"teamId":"1","status":"pending"},
{"teamId":"2","status":"member"},{"teamId":"3","status":"member"}]}';
var obj = JSON.parse(str);
obj['theTeam'].push({"teamId":"4","status":"pending"});
str = JSON.stringify(obj);
最后我们 JSON.stringify obj 回到json
答案 3 :(得分:1)
在我的情况下,我的JSON对象中没有任何现有的Array,因此我必须先创建array元素,然后再推送该元素。
elementToPush = [1, 2, 3]
if (!obj.arr) this.$set(obj, "arr", [])
obj.arr.push(elementToPush)
(此答案可能与该特定问题无关,但可能会有所帮助 别人)
答案 4 :(得分:0)
例如,这里有一个元素,如按钮,用于将项目添加到购物篮以及用于保存在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”, “nameEn”:“Product Name2”,“price”:“76”,“image”:“1461449637106.jpeg”},{“id”:“94”,“nameEn”:“Product Name3”,“price”: “87”, “图像”: “1461449679506.jpeg”}]
执行此操作后,您可以轻松地将数据作为Java中的List发送到服务器
完整的代码示例在这里