我正在保存包含原始数据(缓冲区)的.tmp文件
[{"start_bin_value":"000180000","end_bin_value":"000180999","card_length":"16","card_brand":"006","service_type":"002","card_organization":"003","cdv_validation":"1","issuer_region":"1","bin_country":"840","issuer_bin":"400087","cft_flag":"000","mt_flag":"001","funding_source":"D","restricted_use":"A"},{"start_bin_value":"021502670","end_bin_value":"021502909","card_length":"19","card_brand":"006","service_type":"002","card_organization":"003","cdv_validation":"0","issuer_region":"1","bin_country":"840","issuer_bin":"400087","cft_flag":"000","mt_flag":"001","funding_source":"D","restricted_use":"A"},{"start_bin_value":"221572830","end_bin_value":"221572839","card_length":"16","card_brand":"006","service_type":"002","card_organization":"003","cdv_validation":"0","issuer_region":"1","bin_country":"840","issuer_bin":"400087","cft_flag":"000","mt_flag":"000","funding_source":"D","restricted_use":"A"},{"start_bin_value":"308952000","end_bin_value":"308969999","card_length":"19","card_brand":"006","service_type":"002","card_organization":"003","cdv_validation":"0","issuer_region":"1","bin_country":"840","issuer_bin":"400087","cft_flag":"000","mt_flag":"001","funding_source":"D","restricted_use":"A"}]
现在,我想检查(每次读取这些文件)内容/原始数据是否已损坏/损坏,并且我能够以某种方式对其进行解析
我该怎么做?
谢谢。
答案 0 :(得分:1)
您没有指定损坏/损坏的含义,但是如果您只想检查是否可以解析它,只需使用fs.readFileSync
并使用{{ 3}}围绕try...catch
:
/*
In Node.js you would do:
const fs = require('fs');
const rawData = fs.readFileSync("path/to/data.json");
*/
let rawData = `
[{"start_bin_value":"000180000","end_bin_value":"000180999","card_length":"16","card_brand":"006","service_type":"002","card_organization":"003","cdv_validation":"1","issuer_region":"1","bin_country":"840","issuer_bin":"400087","cft_flag":"000","mt_flag":"001","funding_source":"D","restricted_use":"A"},{"start_bin_value":"021502670","end_bin_value":"021502909","card_length":"19","card_brand":"006","service_type":"002","card_organization":"003","cdv_validation":"0","issuer_region":"1","bin_country":"840","issuer_bin":"400087","cft_flag":"000","mt_flag":"001","funding_source":"D","restricted_use":"A"},{"start_bin_value":"221572830","end_bin_value":"221572839","card_length":"16","card_brand":"006","service_type":"002","card_organization":"003","cdv_validation":"0","issuer_region":"1","bin_country":"840","issuer_bin":"400087","cft_flag":"000","mt_flag":"000","funding_source":"D","restricted_use":"A"},{"start_bin_value":"308952000","end_bin_value":"308969999","card_length":"19","card_brand":"006","service_type":"002","card_organization":"003","cdv_validation":"0","issuer_region":"1","bin_country":"840","issuer_bin":"400087","cft_flag":"000","mt_flag":"001","funding_source":"D","restricted_use":"A"}]
`;
try {
const data = JSON.parse(rawData);
console.log('Data parsed OK!');
} catch(err) {
console.error('Data could not be parsed!');
console.error(err);
}
// Corrupt it so that it's no longer valid JSON:
rawData = rawData.slice(0, -10);
try {
const data = JSON.parse(rawData);
console.log('Data parsed OK!');
} catch(err) {
console.error('Data could not be parsed!');
console.error(err);
}