[英]Formatting json response for jQuery upload Plugin
I am using jQuery Upload File plugin and trying to integrate database to it. 我正在使用jQuery Upload File插件,并尝试将数据库集成到它。 I need to output the json response correct format like that :
我需要像这样输出json响应正确格式:
{"files": [
{
"id": 1,
"name": "picture1.jpg"
},
{
"id": 2,
"name": "picture2.jpg"
}
]}
What I have at the moment : 我目前所拥有的:
[
{
"id": 1,
"name": "picture1.jpg"
},
"id": 2,
"name": "picture2.jpg"
}
]
My php file looks like that : 我的php文件看起来像这样:
$files= array();
$db = new DB;
$query = $db->get_rows("SELECT * FROM `files` ORDER BY `name`");
foreach ($query as $row) {
$file = new stdClass();
$file->id = $row->id;
$file->name = $row->name;
array_push($files,$file);
}
header('Content-type: application/json');
echo json_encode($files);
$files= array();
$db = new DB;
$query = $db->get_rows("SELECT * FROM `files` ORDER BY `name`");
foreach ($query as $row) {
$file = new stdClass();
$file->id = $row->id;
$file->name = $row->name;
array_push($files,$file);
}
header('Content-type: application/json');
echo json_encode(array('files' => $files));
$t = '[{"id": 1, "name": "picture1.jpg"}, {"id": 2, "name": "picture2.jpg"}]';
$t = json_decode($t, true);
$t = array("files" => $t);
echo json_encode($t);
Output: 输出:
{"files":[{"id":1,"name":"picture1.jpg"},{"id":2,"name":"picture2.jpg"}]}
Or with your code simply do: 或只需使用您的代码即可:
$files= array();
$db = new DB;
$query = $db->get_rows("SELECT * FROM `files` ORDER BY `name`");
foreach ($query as $row) {
$file = new stdClass();
$file->id = $row->id;
$file->name = $row->name;
array_push($files,$file);
}
header('Content-type: application/json');
echo json_encode(array("files" => $files));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.