Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用节点JS解析JSON数组时出现问题_Javascript_Arrays_Json_Node.js_Express - Fatal编程技术网

Javascript 使用节点JS解析JSON数组时出现问题

Javascript 使用节点JS解析JSON数组时出现问题,javascript,arrays,json,node.js,express,Javascript,Arrays,Json,Node.js,Express,Node JS和Express新手,目前需要在Node中构建一个小型概念验证API 由于不知道如何最好地解析JSON数组以检索所需数据,从而通过“发送方id”选择适当的消息,目前遇到了一些问题 我已经能够使它与非数组数据一起工作,但无法使它与数组中的数据一起工作 当前用于非数组的代码如下所示。任何关于如何解决这个问题的建议都将不胜感激 JSON { "data_type": "edijson", "payload": { "invoices": [{"invoi

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

由于不知道如何最好地解析JSON数组以检索所需数据,从而通过“发送方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
   })

因为这是一个数组,所以可以循环遍历它

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

如果您可以发布您正在努力处理的json数据,让任何试图帮助您的人都能更清楚地了解这些数据,这可能会更加有益。否则,您是否尝试过在数组中循环并搜索数据?Hi Stuart,完整的Json添加到postHi Abhyudit,感谢您的响应,但在我的示例中,这段代码将放在哪里?在
data=Json.parse(data)之后感谢Abhyudit,正在运行,但得到了节点响应“无法读取未定义”的“forEach”属性?这是因为数据没有任何称为“invoices”的键。应该是data.payload.invoicesb顺便说一下,请尝试console.log()并查看实际存在哪些键。这段代码只是一个示例,您可以有不同的键。但原则仍然是一样的。通过执行
object.property
object[“property”]