TL; DR - 我希望SQL的格式为" test.json" (下面的例子)我想动态分配每个变量,这样根据它有多少条目就会输出那个数量(所以它看起来像下面的输出例子)
我在一个名为" test.json"
的单独文件中{
"12321534232354": {
"server": "2343262364124",
"time": "infinite",
"reason": "not provided"
},
"155149108183695360": {
"server": "2343262364124",
"time": "infinite",
"reason": "not provided"
}
}
我正在使用alasq
模块从这个文件中选择SQL,如下所示:
const alasql = require("alasql");
testfile = require("./test.json");
console.log(alasql("SELECT * FROM ? WHERE server='2343262364124'",[testfile]));
但是,这会返回[],而不是我选择的值。我的最终目标是能够选择一个特定的服务器,并获得每个条目的第一行(在这种情况下" 12321534232354"," 155149108183695360")以及&#34 ;时间"和"原因"并根据它们的数量将它们分配给变量/数组。所以我可以console.log
所有这些值
期望的结果: 例如:
" test.json"包含:
{
"15432213213564": {
"server": "45645645654645",
"time": "infinite",
"reason": "not provided"
},
"4567863243123": {
"server": "45645645654645",
"time": "infinite",
"reason": "not provided"
},
"5763542345345": {
"server": "45645645654645",
"time": "20",
"reason": "test"
}
}
如果我查询服务器" 45645645654645"我希望输出为:(例如:根据此示例)
the user is 12321534232354
the time is infinite
the reason is not provided
the user is 155149108183695360
the time is infinite
the reason is not provided
the user is 5763542345345
the time is 20
the reason is test
这就是为什么我认为我应该动态地将用户,时间,原因分配给变量?
请让我知道如何做到这一点,或者更好的代码示例。
感谢。
答案 0 :(得分:2)
您必须对从json文件加载的对象进行处理。我构建了一个工作样本。我认为alasql无法处理对象,因为它不是数组,实际上它是键值对字典,我的代码片段删除了键,并生成值的数组,然后我可以选择:
const alasql = require("alasql");
var testfile = require("./test.json");
//get array of values of the object loaded from file
var values =Object.values(testfile);
//select
console.log(alasql("SELECT * FROM ? WHERE server = \"45645645654645\"", [values]));
更新:使用Object.values()
简化它我使用了test.json的这个内容:
{
"15432213213564": {
"server": "45645645654645",
"time": "infinite",
"reason": "not provided"
},
"4567863243123": {
"server": "45645645654645",
"time": "infinite",
"reason": "not provided"
},
"5763542345345": {
"server": "45645645654645",
"time": "20",
"reason": "test"
}
}
答案 1 :(得分:1)
我在考虑这样的事情。既然你有一个json文件并希望在console.log中使用javascript对象,那么使用javascript来获取该对象而不是使用SQL是有意义的。
/* Uncomment this so you use the actual json file
const testfile = require("./test.json");
const records = JSON.parse( testfile );
*/
// replace the following records definition by the code above
const records = {
"15432213213564": {
"server": "1234756783612",
"time": "infinite",
"reason": "not provided"
},
"4567863243123": {
"server": "2343262364124",
"time": "infinite",
"reason": "not provided"
},
"5763542345345": {
"server": "2343262364124",
"time": "20",
"reason": "test"
}
};
const findAllServers = ( id_server, records ) => Object.values( records ).filter( record => record.server === id_server );
const all_servers_2343262364124 = findAllServers ( '2343262364124', records );
// will log an array
console.log( all_servers_2343262364124 );
答案 2 :(得分:0)
您也可以尝试使用AlaSQL的SEARCH运算符:
const data = require('./test606a.json');
let res = alasql('SEARCH AS @a \
KEYS() AS @b \
EX(@a->(@b)) \
WHERE(server = ?) \
RETURN(@b AS user, time, reason)\
FROM ?',["45645645654645",data]);
SEARCH运算符旨在遍历复杂的JSON结构。
下面:
a