Arrays 深度复制json简化

Arrays 深度复制json简化,arrays,json,node.js,deep-copy,Arrays,Json,Node.js,Deep Copy,试图复制一个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":"D

试图复制一个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.parse
able,如果是,则在对象中包含它们的键/值对

如果是这样的话,一个简单的try/catch就可以了。您还可以在
JSON.parse
之后添加另一个检查,以验证解析的输出实际上是一个对象,或者过滤掉某些键值,等等

const src={
__createdAt:“2018-07-30T08:19:32.523Z”,订单id:'12345',退款:空,订单已验证:空,正在处理:空,位置id:空,
用户信息:“{”countrySelect:“DE”,“postalCode:“64289”,“ShippingCountry:“Germany”,“City:“Darmstadt”,“GooglePlace:“Darmstadt Germany”,“ShippingRegion:“Hesse”,“CustomerEmail:”myemail@gmail.com"}',
付款:空,发货信息:1437,taxInfo:0,订单总数:5712,订单重量:0,订单备注:“”
}
函数copyExpand(目标、源){
for(让k进入源){
if(源的类型[k]=“字符串”){
试一试{
让json=json.parse(source[k]);
copyExpand(目标,json);
}捕获(e){
目标[k]=源[k];
}
}else目标[k]=源[k];
}
}
常量tgt={};
copyExpand(tgt、src);

console.log(tgt)您需要提供更多的上下文,JSON字符串是否有1级的深度?是的。可以有多个JSON字符串,但深度为一级。是否总是包含JSON的
userInfo
属性?否,但不超过2或3个。这是它必须很少硬编码的部分。如果出现“userInfo”,我们将从中复制2 3个预定义的键/值。