我有一个包含大约100个JSON格式条目的数据集:
[
{"name":"Julia","state":"CA","income":66000},
{"name":"Peter","state":"NY","income":35000},
{"name":"Greg","state":"CA","income":0},
{"name":"Celine","state":"MN","income":27000}
]
由于数据集不会经常更改,因此我希望将数据集与代码保存在一个git repo中,并消除对数据库的依赖。
典型查询为return all entries where the name starts with a G
或return all entries where the sate equals CA
。一种方法是:
使用file_get_contents()
(
如果操作系统支持,它将使用内存映射技术来提高性能。
$data = file_get_contents("data.json");
然后我将JSON数据解析为数组:
$json = json_decode($data);
最后,我遍历所有数据以检查它是否等于给定值
...
$json[$i]->{"state"} == "CA";
...
现在我有一些问题:
json_decode($data)
时都会执行。有没有办法直接将JSON-Data读入自己的类(默认情况下它是某种标准类)?
object(stdClass)#1 (3) {
["name"]=>
string(5) "Julia"
["state"]=>
string(2) "CA"
["income"]=>
int(66000)
}
有没有更好的方法来解决任务[复杂的对象,而不是数据库](我猜有,......)