2016-11-03 12 views
-2

JSとExpressの新機能ノードに小規模な概念証明APIを構築する必要があります。ノードでJSON配列を解析する際の問題JS

現在、「sender_id」で適切なメッセージを選択するために必要なデータを取得するためにJSON配列を解析する方法に関する知識が不足しているため、問題が発生しています。

私はそれを非配列データで動作させることはできましたが、配列内のデータを扱うことはできません。

現在、非配列で動作するコードは次のとおりです。どのようにこの問題を解決する上での指針をいただければ幸いです。

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

あなたが苦労しているjsonデータを投稿して助けようとしている人に明確にすることができれば、より有益かもしれません。それ以外の場合は、配列をループしてデータを検索しようとしましたか? – Stuart

+0

こんにちはスチュアート、完全なJsonは投稿に追加 – mig

答えて

1

は、あなたがそれをループすることができ、配列です。

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

こんにちはAbhyudit、応答のおかげで、このコードは私の例に配置されるだろうか? – mig

+0

'data = JSON.parse(data);の後 –

+0

ありがとうございますAbhyudit、実行中ですが、ノードの応答が 'プロパティを読み取れません' forEach 'undefined'? – mig