【问题标题】:How parse the json arraylist in node.js? [duplicate]如何解析node.js中的json数组列表? [复制]
【发布时间】:2018-04-24 02:43:05
【问题描述】:

我想在节点 js 中解析以下类型的数据,以便对它们执行操作。 数据:-

{"orderSummary":[ {
      "name":"Wine",
      "ProductPrice":"500",
      "ProductQuantity":"2",
      "ProductCost":"1000",
      "SellerId":"2"

   },{
      "name":"Whisky",
      "ProductPrice":"500",
      "ProductQuantity":"2",
      "ProductCost":"1000",
      "SellerId":"2"

   }]
}

这是我用来解析数据的代码 sn-p:-

router.post('/createorder',function(req,res){
    console.log(req.body);
     var data=req.body;
    var obj=JSON.parse(data);
    res.send(obj);
});

谁能告诉我除了循环(因为我将实现它)以解析数据并对其执行操作之外我还能做什么?

【问题讨论】:

  • 我已经完成了这个,但我仍然无法找到确切的答案,因为我的数据来自应用程序作为输入数据中显示的键值参数
  • @atulkumar 请更清楚您要完成什么。
  • 这取决于你想要做什么解析操作......具体。
  • 我想访问元素并将其存储在数据库中

标签: javascript json node.js


【解决方案1】:
{"orderSummary":[ {
  "name":"Wine",
  "ProductPrice":"500",
  "ProductQuantity":"2",
  "ProductCost":"1000",
  "SellerId":"2"

},{
  "name":"Whisky",
  "ProductPrice":"500",
  "ProductQuantity":"2",
  "ProductCost":"1000",
  "SellerId":"2"

}]
}

假设这个 json 数据在正文中 所以只需存储在 var var data=req.body.orderSummary 现在遍历它。 这是最好的方式,因为你有 json 数据。所以你需要实现它。

【讨论】:

  • 我已经尝试过了,我收到以下错误“SyntaxError: Unexpected token # in JSON at position 0”
  • var data=req.body.orderSummary ; for (var i=0;i
  • 它适合你吗?
  • 请点赞我的回答。
  • 已经这样做了
猜你喜欢
  • 1970-01-01
  • 2016-02-09
  • 1970-01-01
  • 2018-07-22
  • 2015-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多