我正在使用Node.js从XML文件中读取数据。但是,当我尝试将文件中的数据与文字进行比较时,尽管它们看起来是相同的,但它们并不匹配:
const parser: xml2js.Parser = new xml2js.Parser();
const suggestedMatchesXml: any
= fs.readFileSync(`${inputSuggMatchXmlFile}`, 'utf8');
parser.parseString(suggestedMatchesXml, (_parseErr: any, result: any) => {
// console.debug(`result = ${util.inspect(result, false, null)}`);
suggestedMatchesObjFromXml = JSON.parse(JSON.stringify(result));
// console.debug(`suggestedMatchesObjFromXml BEFORE = ${JSON.stringify(suggestedMatchesObjFromXml)}`);
});
const destinations: Array<any> = suggestedMatchesObjFromXml.suggestedmatches.destination;
let docIdForUrl: string | undefined;
_.each(destinations, (destination: any) => {
const { url }: { url: string } = destination;
if (!destination.docId) {
console.debug(`processInputSmXmlFile(): url = ${url} ; index = ${_.indexOf(url, 'meetings')}`);
日志如下:
processInputSmXmlFile(): url = https://apps.na.collabserv.com/meetings/ ; index = -1
我不确定这是怎么发生的,除非其中一个字符串是unicode,而另一个不是。
我如何才能以这样或那样的方式转换,以使数据匹配?
发布于 2018-09-25 04:17:37
因为我正在做一个JSON.parse()
,所以url
不是一个字符串--它是一个对象。当我做了一个_.toString(url)
,并用_.includes(url, 'meetings')
替换了_.indexOf(url, 'meetings')
(因为_.includes(url, 'meetings')
indexOf()
只适用于数组),现在一切都正常了。
https://stackoverflow.com/questions/52481632
复制相似问题