使用 javascript 更新 json 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8998683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update a json object using javascript
提问by Diver Dan
I am having problems trying to sort this on my own.
我在尝试自行排序时遇到问题。
I have a hidden field containing a small amount of json.
我有一个包含少量 json 的隐藏字段。
I populate a variable using
我使用填充变量
$(document).ready(function() {
var data = $("#result").text();
var j = JSON.parse(data);
j.my_item.total_price==="2222";
console.log(j.my_item.total_price);
});
the variable j is showing the correct data, I just don't have a clue how to update the total_price
变量 j 显示了正确的数据,我只是不知道如何更新 total_price
Can anyone suggest what I need to do to enable me to update total_price?
谁能建议我需要做什么才能更新 total_price?
回答by Niet the Dark Absol
You can assign to an object property just like any other variable:
您可以像任何其他变量一样分配给对象属性:
j.my_item.total_price = "2222";
Or the alternative (array-like) syntax:
或者替代(类数组)语法:
j['my_item']['total_price'] = "2222";
Or mix-and-match:
或混搭:
j.my_item['total_price'] = "2222";
j['my_item'].total_price = "2222";
回答by mowwwalker
$(document).ready(function() {
var data = $("#result").text();
var j = JSON.parse(data);
j.my_item.total_price="2222";
console.log(j.my_item.total_price);
});
===
isn't an assignment operator, it's a type-strict comparison operator.
===
不是赋值运算符,而是类型严格的比较运算符。
See here:
看这里: