【问题标题】:Issues Parsing JSON Array with Node JS使用 Node JS 解析 JSON 数组的问题
【发布时间】:2016-11-03 11:29:41
【问题描述】:

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
   })

【问题讨论】:

  • 如果您可以发布您正在努力处理的 json 数据,以便让任何试图提供帮助的人更清楚,这可能会更有益。否则您是否尝试过遍历数组并搜索数据?
  • 嗨 Stuart,已将完整的 Json 添加到帖子中

标签: javascript arrays json node.js express


【解决方案1】:

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

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

【讨论】:

  • 嗨,Abhyudit,感谢您的回复,但在我的示例中,这段代码应该放在哪里?
  • data = JSON.parse(data);之后
  • 感谢 Abhyudit,正在运行但收到“无法读取未定义的属性 'forEach' 的节点响应”?
  • 那是因为数据没有任何称为“发票”的键。应该是 data.payload.invoices
  • 顺便说一下,请尝试 console.log() 看看实际上有哪些键。这段代码只是一个例子,你可以有不同的键。但校长保持不变。您可以通过 object.propertyobject["property"] 访问对象的任何属性
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-22
  • 1970-01-01
  • 2020-01-20
  • 1970-01-01
  • 1970-01-01
  • 2018-10-01
  • 2014-10-08
相关资源
最近更新 更多