0
我有一些看起来像这样的JSON。将JSON数组写入对象
[
{
"items": [
{
"id": "xxxx",
"description": {
"style": "",
"specs": ""
}
},
{
"id": "xxxx",
"description": {
"style": "",
"specs": ""
}
}
],
"category": "xxxxxx",
"name": "xxxxxx"
},
{
"items": [
{
"id": "xxxx",
"description": {
"style": "",
"specs": ""
}
},
{
"id": "xxxx",
"description": {
"style": "",
"specs": ""
}
}
],
"category": "xxxxxx",
"name": "xxxxxx"
}
]
它被作为'newItem'传递给我。而在现有的代码被设置两次我的工作:
that.cart.addItem(newItem.toObject());
及更高版本:
that.origCart.replaceWith(that.cart);
这个传统一直很好,当“项目”是一个平坦的列表。不过,上面的JSON是新的JSON格式,因此我需要为每个类别/名称传递“项目”。
里面的“for”循环我有这样的输出什么,我需要为每一个(我需要让他们分开。)
newItem.items
然而,当我尝试执行,下面我得到一个类型错误。 (是的,我将有多个“车”,一旦我得到这个工作。)
that.cart.addItem(newItem.items.toObject());
TypeError: newItem.items.toObject is not a function
我能做些什么让“newItem.items”为适当的类型,或者我可以代替什么操作正确地设置这些设置?我尝试了一些我在网上找到的东西,但没有成功,现在我正在转向专家。只是不是我的特长(还)。
对不起,我发布了这个多任务,我搞砸了。我修正了上面的两行。 that.cart.addItem(newItem.toObject()); that.cart.addItem(newItem.items.toObject()); – DSel
这最终奏效了。我改成了例子:that.cart.addItem(newItem.items [0]);我得到每个项目的正确数据。可以发誓我尝试了这个(因此为什么toObject最初失踪)。感谢你把我推向正确的方向。 – DSel