【发布时间】: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