【发布时间】:2018-08-26 22:05:03
【问题描述】:
试图复制一个 json 对象,但是当其中有一个字符串时,我只需要其中的几个键/值对并将其复制到另一个 json 对象(简化); JSON中的数据是这样的
{ __createdAt: "2018-07-30T08:19:32.523Z",
orderid: '12345',
refund: null,
order_verified: null,
in_process: null,
location_id: null,
userInfo: '{"countrySelect":"DE","postalCode":"64289","ShippingCountry":"Germany","City":"Darmstadt","GooglePlace":"Darmstadt Germany","ShippingRegion":"Hesse","CustomerEmail":"myemail@gmail.com"}',
payment: null,
shippingInfo: 1437,
taxInfo: 0,
orderTotal: 5712,
order_weight: 0,
order_notes: '' }
我复制后想要达到的结果是这样的。
{ __createdAt: "2018-07-30T08:19:32.523Z",
orderid: '12345',
refund: null,
order_verified: null,
in_process: null,
location_id: null,
countrySelect:"DE",
ShippingCountry:"Germany",
City:"Darmstadt",
CustomerEmail:"myemail@gmail.com",
payment: null,
shippingInfo: 1437,
taxInfo: 0,
orderTotal: 5712,
order_weight: 0,
order_notes: '' }
我不知道什么数据会来自数据库,但只要它包含字符串,我就可以对其进行硬编码以从 json 中的字符串获取特定值。 尝试过深拷贝,但无法正确完成。这并不是说我没有尝试过完成这项工作,而是无法想出一种方法让它更通用而不是硬编码。任何帮助,将不胜感激。
【问题讨论】:
-
您需要提供更多上下文,JSON 字符串是不是我曾经的 1 级深度?
-
是的。可能有多个 JSON 字符串,但只有一层。
-
是否总是
userInfo属性包含 JSON? -
不,但不超过 2 或 3 个。这是必须很少硬编码的部分。
-
如果出现“userInfo”,我们会从中复制 2 3 个预定义的键/值。
标签: arrays json node.js deep-copy