使用Node JS解析JSON数组的问题

时间:2016-11-03 11:29:42

标签: javascript arrays json node.js express

Node JS和Express的新手,目前需要在Node中构建一个小概念证明API。

由于缺乏关于如何最好地解析JSON数组以检索所需数据以通过“sender_id”选择适当消息的知识,目前遇到问题。

我已经能够使它与非数组数据一起使用但无法使其与数组中的数据一起使用。

与非数组一起使用的当前代码如下。任何有关如何解决此问题的建议都将不胜感激。

JSON

   {
    "data_type": "edijson",
    "payload": {
      "invoices": [{"invoice_6582":
    {
     "sender_id": 5060043358032,
     "buyer_id" : 5055185400478,
     "message_ref": 135353677,
     "document_type": "Commercial",
     "document_name": "MRCHI",
     "document_no": 6582,
     "state": "partial",
     "total_amount": 1008.00,
     "paid_at": "null",
     "invoice_date": "2012-01-30T00:00:00",
     "tax_date": "2012-01-30T00:00:00",
     "amount_due": 1008.00,
     "invoice_no": "6582",
     "order_no": 9899073,
     "supplier_no": "5060043358032",
     "delivery_party" : 5010251999898,
     "vat_reg": 793728185,
   "charges": [
        {
           "created_at": "20120130",
           "item_id": 5010251685524,
           "item_qty": 140,
           "unit_price": 7.20,
           "vat_type": "VAT",
           "vat_rate": 20.00,
           "currency_id" : "GBP",
           "total_amont": 1008.00,
           "type": "Charge"
         }],
   "totals": [
        {
            "invoice_amount": 1008.00,
            "total_amount": 1008.00,
            "tax_amount": 201.60,
            "taxable_amount": 1008.00,
            "allowances": 0.00
         }

        ]
      }
   }
,
{"invoice_6788":
    {
     "sender_id": 5060043358032,
     "buyer_id" : 5055185400478,
     "message_ref": 10636000100020,
     "document_type": "Commercial",
     "document_name": "MRCHI",
     "document_no": 6582,
     "state": "partial",
     "total_amount": 1008.00,
     "paid_at": "null",
     "invoice_date": "2012-01-30T00:00:00",
     "tax_date": "2012-01-30T00:00:00",
     "amount_due": 1008.00,
     "invoice_no": "6788",
     "order_no": 9899073,
     "supplier_no": "5060043358032",
     "delivery_party" : 5010251999898,
     "vat_reg": 793728185,
   "charges": [
        {
           "created_at": "20120130",
           "item_id": 5010251685524,
           "item_qty": 140,
           "unit_price": 7.20,
           "vat_type": "VAT",
           "vat_rate": 20.00,
           "currency_id" : "GBP",
           "total_amont": 1008.00,
           "type": "Charge"
         }],
   "totals": [
        {
            "invoice_amount": 1008.00,
            "total_amount": 1008.00,
            "tax_amount": 201.60,
            "taxable_amount": 1008.00,
            "allowances": 0.00
         }

        ]
      }
   }
,
{"invoice_7786":
    {
     "sender_id": 5060043358032,
     "buyer_id" : 5055185400478,
     "message_ref": 10636000100020,
     "document_type": "Commercial",
     "document_name": "MRCHI",
     "document_no": 6582,
     "state": "partial",
     "total_amount": 1008.00,
     "paid_at": "null",
     "invoice_date": "2012-01-30T00:00:00",
     "tax_date": "2012-01-30T00:00:00",
     "amount_due": 1008.00,
     "invoice_no": "7786",
     "order_no": 9899073,
     "supplier_no": "5060043358032",
     "delivery_party" : 5010251999898,
     "vat_reg": 793728185,
   "charges": [
        {
           "created_at": "20120130",
           "item_id": 5010251685524,
           "item_qty": 140,
           "unit_price": 7.20,
           "vat_type": "VAT",
           "vat_rate": 20.00,
           "currency_id" : "GBP",
           "total_amont": 1008.00,
           "type": "Charge"
         }],
   "totals": [
        {
            "invoice_amount": 1008.00,
            "total_amount": 1008.00,
            "tax_amount": 201.60,
            "taxable_amount": 1008.00,
            "allowances": 0.00
         }

        ]
      }
   }
,
{"invoice_4567":
    {
     "sender_id": 5060043358032,
     "buyer_id" : 5055185400478,
     "message_ref": 10636000100020,
     "document_type": "Commercial",
     "document_name": "MRCHI",
     "document_no": 6582,
     "state": "partial",
     "total_amount": 1008.00,
     "paid_at": "null",
     "invoice_date": "2012-01-30T00:00:00",
     "tax_date": "2012-01-30T00:00:00",
     "amount_due": 1008.00,
     "invoice_no": "4567",
     "order_no": 9899073,
     "supplier_no": "5060043358032",
     "delivery_party" : 5010251999898,
     "vat_reg": 793728185,
   "charges": [
        {
           "created_at": "20120130",
           "item_id": 5010251685524,
           "item_qty": 140,
           "unit_price": 7.20,
           "vat_type": "VAT",
           "vat_rate": 20.00,
           "currency_id" : "GBP",
           "total_amont": 1008.00,
           "type": "Charge"
         }],
   "totals": [
        {
            "invoice_amount": 1008.00,
            "total_amount": 1008.00,
            "tax_amount": 201.60,
            "taxable_amount": 1008.00,
            "allowances": 0.00
         }

        ]
      }
   }
] 
}
}

节点

    var express = require('express');
    var app = express();
    var fs = require("fs");

    app.get('/v1/invoice/:sender_id', function (req, res) {
    // First read existing invoices.
   fs.readFile( __dirname + "/" + "Invoice.json", 'utf8', function (err, data) {
      data = JSON.parse( data );
      var invoice = data["invoice_" + req.params.sender_id]
      console.log( invoice );
      res.end( JSON.stringify(invoice));
      });
    })

   var server = app.listen(8081, function () {
   var host = server.address().address
   var port = server.address().port
   })

1 个答案:

答案 0 :(得分:1)

由于这是一个数组,你可以遍历它。

data.payload.invoices.forEach(function(invoice){
    if (invoice.invoice_no === req.params.id) {
        // do stuff
    }
});