本文介绍了在JSON中更新elemint将更新所有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我解析了一个JSON对象,我试图向下导航至SHIPPINGCOMMENTS并更新它,但是当我这样做时,它将更新具有该名称的所有单元,而不只是一个名称.
I have a JSON object parsed and I am trying to navigate down to SHIPPINGCOMMENTS and update it, but when I do, it updates all cells with that name instead of just the one.
{
"id": 1402846607011,
"status": "unsaved",
"accounts": [
{
"compid": 919759,
"compname": null,
"products": [
{
"BCINUM": "539504",
"ITEMUNIT": "EA",
"ORDERDETAILS": [
{
"SHIPDATEID": "69230",
"SHIPPERIODID": "2096",
"QUANTITY": "1"
},
{
"SHIPDATEID": "69231",
"SHIPPERIODID": "2096",
"QUANTITY": "2"
}
],
"SHIPPINGCOMMENTS": "sooner"
}
]
},
{
"compid": 920001,
"compname": null,
"products": [
{
"BCINUM": "539504",
"ITEMUNIT": "EA",
"ORDERDETAILS": [
{
"SHIPDATEID": "69230",
"SHIPPERIODID": "2096",
"QUANTITY": "1"
},
{
"SHIPDATEID": "69231",
"SHIPPERIODID": "2096",
"QUANTITY": "2"
}
],
"POTEXT": "",
"SHIPPINGCOMMENTS": "sooner"
}
]
}
]
}
这是我要遍历的代码:
function updateComments(compID,bcinum,comment) {
var accounts = runningOrders.accounts;
var n = accounts.length;
for (i = 0; i < n; i++) {
if (accounts[i].compid == compID) {
var p = accounts[i].products.length;
for (ii = 0; ii < p; ii++) {
if (accounts[i].products[ii].BCINUM == bcinum) {
accounts[i].products[ii].SHIPPINGCOMMENTS = comment;
}
}
}
}
}
函数调用为:
updateComments(919759,539504,sooner);
推荐答案
两个潜在问题:
- 在计算n时,您实际上不是在计算acconts数组的大小吗? (JSON)对象的名称是什么?
- 在示例函数调用中,您正在将两个对象中的名称更改为已经定义的名称.这是一个错误吗?
这段代码在node.js中对我有用:
This code is working for me in node.js:
function updateComments(compID,bcinum,comment) {
var n = accounts.accounts.length;
console.log(n);
console.log(accounts['accounts'][0].products);
console.log(accounts['accounts'][1].products);
for (i = 0; i < n; i++) {
console.log('testing ', i, accounts.accounts[i].compid)
if (accounts.accounts[i].compid == compID) {
var p = accounts.accounts[i].products.length;
console.log('Found compid', i, compID);
for (ii = 0; ii < p; ii++) {
if (accounts.accounts[i].products[ii].BCINUM == bcinum) {
console.log('Found bcinum', ii, bcinum)
accounts.accounts[i].products[ii].SHIPPINGCOMMENTS = comment;
}
}
}
}
console.log(accounts['accounts'][0].products);
console.log(accounts['accounts'][1].products);
}
accounts = {
"id": 1402846607011,
"status": "unsaved",
"accounts":
[
{
"compid": 919759,
"compname": null,
"products": [
{
"BCINUM": "539504",
"ITEMUNIT": "EA",
"ORDERDETAILS": [
{
"SHIPDATEID": "69230",
"SHIPPERIODID": "2096",
"QUANTITY": "1"
},
{
"SHIPDATEID": "69231",
"SHIPPERIODID": "2096",
"QUANTITY": "2"
}
],
"SHIPPINGCOMMENTS": "sooner"
}
]
},
{
"compid": 920001,
"compname": null,
"products": [
{
"BCINUM": "539504",
"ITEMUNIT": "EA",
"ORDERDETAILS": [
{
"SHIPDATEID": "69230",
"SHIPPERIODID": "2096",
"QUANTITY": "1"
},
{
"SHIPDATEID": "69231",
"SHIPPERIODID": "2096",
"QUANTITY": "2"
}
],
"POTEXT": "",
"SHIPPINGCOMMENTS": "sooner"
}
]
}
]
}
这篇关于在JSON中更新elemint将更新所有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!