我需要创建预先请求脚本,根据数据是通过json还是csv文件来调整行为。
是否有任何运行时机制来确定数据文件类型或数据文件的名称?
帮助我做以下事情的事情,
if (data.fileType === "json") {
//data interpretation as per json hierarchy
} else {
//data interpretation as per csv structure
}
在这里,我可以用什么来代替data.fileType
?
或者是否可以找出data.fileName
?
答案 0 :(得分:1)
您可以尝试将data
解析为json。
var parsedData;
try {
parsedData = JSON.parse(data); // Will throw an error if data not JSON
// Your logic to parse data as JSON goes here
} catch (e) {
parsedData = data; // data is not JSON, check for CSV
// Your logic to parse as CSV goes here
}