gpt4 book ai didi

javascript - 通过 URL 将大型 Xml 文件转换为 JSON

转载 作者:行者123 更新时间:2023-12-01 00:54:15 25 4
gpt4 key购买 nike

在我的家庭任务中,我被要求在服务器 Node.js 上开发 Restful api。我的问题是使用 URL 将巨大的 XML 转换为 JSON 格式。

我尝试使用不同的目录,例如 xml-stream、xml2json。我在网上找到了一个可以为我做这件事的函数,但结果很奇怪。我使用了以下指南:https://antrikshy.com/blog/fetch-xml-url-convert-to-json-nodejs

var express = require('express');
var router = express.Router();

// Parse XML to JSON.
var parseString = require('xml2js').parseString;
var https = require('https');

/* GET home page. */
router.get('/', function(req, res, next) {

function xmlToJson(url, callback) {
var req = https.get(url, function(res) {
var xml = '';

res.on('data', function(chunk) {
xml += chunk;
});

res.on('error', function(e) {
callback(e, null);
});

res.on('timeout', function(e) {
callback(e, null);
});

res.on('end', function() {
parseString(xml, function(err, result) {
callback(null, result);
});
});
});
}

var url = "https://www.boi.org.il/he/BankingSupervision/BanksAndBranchLocations/Lists/BoiBankBranchesDocs/snifim_dnld_he.xml";
xmlToJson(url, function(err, data) {
if (err) {
return console.err(err);
}
res.json(JSON.stringify(data, null, 2));
});

});

module.exports = router;

我期望得到有效的 JSON 输出,但得到了错误的输出。

"{\n  \"BRANCHES\": {\n    \"$\": {\n      \"xmlns:ns0\": \"BOI_snifim_he\"\n    },\n    \"BRANCH\": [\n      {\n        \"Bank_Code\": [\n          \"13\"\n        ],\n        \"Bank_Name\": [\n          \"13001-בנק אגוד לישראל בע\\\"מ\"\n        ],\n        \"Branch_Code\": [\n          \"142\"\n        ],\n        \"Branch_Name\": [\n          \"מבשרת\"\n        ],\n        \"Branch_Address\": [\n          \"החושן 6 9079562\"\n        ],\n        \"City\": [\n          \"מבשרת ציון\"\n        ],\n        \"Zip_Code\": [\n          \"9079562\"\n        ],\n        \"POB\": [\n          \"\"\n        ],\n        \"Telephone\": [\n          \"1-599-599-142\"\n        ],\n        \"Fax\": [\n          \"02-5706001\"\n        ],\n        \"Free_Tel\": [\n          \"\"\n        ],\n        \"Handicap_Access\": [\n          \"כן\"\n        ],\n        \"day_closed\": [\n          \"יום א\"\n        ],\n        \"Branch_Type\": [\n          \"מיוחד\"\n        ],\n        \"Date_Open\": [\n          \"02/11/2004\"\n        ],\n        \"Date_Closed\": [\n          \"26/12/2016\"\n        ],\n        \"Merge_Bank\": [\n          \"\"\n        ],\n        \"Merge_Branch\": [\n          \"\"\n        ],\n        \"X_Coordinate\": [\n          \"\"\n        ],\n        \"Y_Coordinate\": [\n          \"\"\n        ]\n      },\n      {\n        \"Bank_Code\": [\n          \"13\"\n        ],\n        \"Bank_Name\": [\n          \"13001-בנק אגוד לישראל בע\\\"מ\"\n        ],\n        \"Branch_Code\": [\n          \"123\"\n        ],\n        \"Branch_Name\": [\n          \"אגוד ישיר\"\n        ],\n        \"Branch_Address\": [\n          \"אבא הילל סלבר 13 \"\n        ],\n        

最佳答案

I expected a valid JSON output

您获得了有效的 JSON 输出。

参见the documentation :

res.json([body])
Sends a JSON response. This method sends a response (with the correct content-type) that is the parameter converted to a JSON string using JSON.stringify().

The parameter can be any JSON type, including object, array, string, Boolean, number, or null, and you can also use it to convert other values to JSON.

res.json(null)
res.json({ user: 'tobi' })
res.status(500).json({ error: 'message' })

您正在使用 JSON.stringify() 将数据转换为 JSON 字符串。

然后,您将该字符串传递给 res.json() 并获取 JSON 的 JSON 编码表示形式。

删除JSON.stringify。然后 res.json() 会将数据编码为 JSON 一次。

关于javascript - 通过 URL 将大型 Xml 文件转换为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56723870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com