自动递增ID JSON [英] Auto increment id JSON
问题描述
我正在制作RESTful Web服务,并且我希望发布到JSON文件的项目具有ID.我到处都在搜索,但找不到有关该操作的任何信息.
I'm making a RESTful webservice and I want the items that are posted to the JSON file to have an Id. I've been searching everywhere but couldn't find anything on how to do this.
JSON看起来像这样
The JSON looks like this
[
{
"id": "2",
"title": "Hello World",
"artist": "John Smith",
"genre": "pop",
"week": "4",
"highest_rating": "3",
"year": "2014",
"youtube": "www",
"links": [
{
"rel": "self",
"href": ""
},
{
"rel": "collection",
"href": ""
}
]
},
{
"id": "3",
"title": "Lorem Ipsum",
"artist": "John Smith",
"genre": "pop",
"week": "4",
"highest_rating": "3",
"year": "2014",
"youtube": "www",
"links": [
{
"rel": "self",
"href": ""
},
{
"rel": "collection",
"href": ""
}
]
}
]
此文件中的ID是硬编码的. 有人可以告诉我如何通过自动增量获取ID
The id's in this file are hardcoded. Can someone please tell me how to get id's with auto increment
修改 我看不出我在解释我想做什么.
Edit I see I've been unclear in explaining what I want to do.
发布到Web服务的数据存储在.json文件中.我没有使用数据库. 因此,在PHP中,这就是我正在做的事情.
Data that is posted to the webservice, is stored in a .json file. I'm not using a DB. So in PHP, this is what I'm doing.
$file = file_get_contents("data.json");
$data = json_decode($file, true);
$data[] = array(
'id'=>$_POST["id"],
'title'=>$_POST["title"],
'artist'=>$_POST["artist"],
'genre'=>$_POST["genre"],
'week'=>$_POST["week"],
'highest_rating'=>$_POST["highest_rating"],
'year'=>$_POST["year"],
'youtube'=>$_POST["youtube"],
"links"=> [ array(
"rel"=>"self",
"href"=>""
),
array(
"rel"=>"collection",
"href"=>""
)
]
);
file_put_contents('data.json',json_encode($data));
因此,目前该ID是通过POST给出的,但我希望它以自动递增的方式给出. 我希望这可以使我的问题更清楚.
So at the moment the id is given through a POST but I want it to be given with auto-increment. I hope this makes my question clearer.
推荐答案
这取决于您的应用程序.如果您正在使用数据库,则显然应该使用数据库主键值,但是如果您将信息存储在简单的.json文件中,则必须自己声明每个行ID.获取最后一个并增加它.
It depends on your application. If you're using DB you obviously should use DB primary key value, but if you're store your info in simple .json file you must declare each row id yourself. Get the last one and increment it.
我强烈建议您将此数据存储在DB中而不是文件中.您永远不会有交易问题(并发写入文件).
I'm strongly recommend you to store this data in DB not in file. You'll never have problems with transactions (concurrent writing to file).
更新
确定,具体取决于您的代码:
OK, depending on your code:
$file = file_get_contents("data.json");
$data = json_decode($file, true);
// Get last id
$last_item = end($data);
$last_item_id = $last_item['id'];
$data[] = array(
'id' => ++$last_item_id,
'title' => $_POST["title"],
'artist' => $_POST["artist"],
'genre' => $_POST["genre"],
'week' => $_POST["week"],
'highest_rating' => $_POST["highest_rating"],
'year' => $_POST["year"],
'youtube' => $_POST["youtube"],
"links" => [
array(
"rel"=>"self",
"href"=>""
),
array(
"rel"=>"collection",
"href"=>""
)
]
);
file_put_contents('data.json',json_encode($data), LOCK_EX);
我们正在使用上方的 LOCK_EX 标志来 在写入过程中获取文件的排他锁 .
We're using LOCK_EX flag above to acquire an exclusive lock on the file while proceeding to the writing.
如果您不能使用数据库,我认为使用 flock()函数:
If you can't use DB I think it's more safely to read and write file with flock() function:
$filename = "data.json";
$filesize = filesize($filename);
$fp = fopen($filename, "r+");
if (flock($fp, LOCK_EX))
{
$data = json_decode(fread($fp, $filesize), true);
// Get last id
$last_item = end($data);
$last_item_id = $last_item['id'];
$data[] = array(
'id' => ++$last_item_id,
'title' => 1,
'artist' => 2,
'genre' => 3,
'week' => 4,
'highest_rating' => 5,
'year' => 6,
'youtube' => 7,
"links" => [
array(
"rel" => "self",
"href" => ""
),
array(
"rel" => "collection",
"href" => ""
)
]
);
fseek($fp, 0);
ftruncate($fp, 0);
fwrite($fp, json_encode($data));
flock($fp, LOCK_UN);
}
else
{
echo "Unable to lock file";
}
fclose($fp);
这篇关于自动递增ID JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!