我正在尝试使用查询引擎 SQLike我正在努力解决基本概念。
我用作我的数据源的JSON来自我的PHP代码,如下所示:
var placesJSON=<? echo json_encode($arrPlaces) ?>;
以下是JSON示例:
var placesJSON=[{"id":"100","name":"Martinique","type":"CTRY"},{"id":"101","name":"Mauritania","type":"CTRY"},{"id":"102","name":"Mauritius","type":"CTRY"},{"id":"103","name":"Mexico","type":"CTRY"},{"id":"799","name":"Northern Mexico","type":"SUBCTRY"},{"id":"800","name":"Southern Mexico","type":"SUBCTRY"},{"id":"951","name":"Central Mexico","type":"SUBCTRY"},{"id":"104","name":"Micronesia, Federated States","type":"CTRY"},{"id":"105","name":"Moldova","type":"CTRY"}];
我理解(通过此reference)我首先需要解压缩我的JSON:
var placesData = SQLike.q(
{
Unpack: placesJSON,
Columns: ['id','name','type']
}
)
下一步是查询结果:
var selectedPlaces = SQLike.q(
{
Select: ['*'],
From: placesData,
OrderBy: ['name','|desc|']
}
最后,要在浏览器中显示结果,我应该使用以下内容:
document.getElementById("myDiv").innerHTML=selectedPlaces[0].name
这不起作用。我得到的错误是:selectedPlaces [0] .name未定义。
我很确定我错过了非常简单的事情。任何提示?
答案 0 :(得分:1)
“Unpack”将数组数组(如[["John", "Peterson", 38, 28000], ["Vicki", "Smith", 43, 89000]]
)转换为对象数组。由于您的Json已采用此格式,因此无需将其解压缩。