如何解析node.js中的json arraylist?

时间:2017-11-10 19:44:20

标签: javascript json node.js

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

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

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

   }]
}

这是我用于解析数据的代码片段: -

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

任何人都可以告诉我除了循环(我将实现它)之外我还能做什么来解析数据并对其执行操作?

1 个答案:

答案 0 :(得分:0)

{"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数据。所以你需要实现它。