Php 将服务器中的图像信息转换为json
需要帮助的朋友们,我想把从数据库检索到的图像信息转换成json格式。 下面是从数据库获取图像信息的代码Php 将服务器中的图像信息转换为json,php,json,Php,Json,需要帮助的朋友们,我想把从数据库检索到的图像信息转换成json格式。 下面是从数据库获取图像信息的代码 // Query for a list of all existing files $sql = 'SELECT `id`, `name`, `mime`, `size`, `created` FROM `file`'; $result = $dbLink->query($sql); // Check if it was successfull if($result) { // Mak
// Query for a list of all existing files
$sql = 'SELECT `id`, `name`, `mime`, `size`, `created` FROM `file`';
$result = $dbLink->query($sql);
// Check if it was successfull
if($result) {
// Make sure there are some files in there
if($result->num_rows == 0) {
echo '<p>There are no files in the database</p>';
}
else {
// Print the top of a table
echo '<table width="100%">
<tr>
<td><b>Name</b></td>
<td><b>Mime</b></td>
<td><b>Size (bytes)</b></td>
<td><b>Created</b></td>
<td><b> </b></td>
</tr>';
// Print each file
while($row = $result->fetch_assoc()) {
echo "
<tr>
<td>{$row['name']}</td>
<td>{$row['mime']}</td>
<td>{$row['size']}</td>
<td>{$row['created']}</td>
<td><a href='get_file.php?id={$row['id']}'>Download</a></td>
</tr>";
}
// Close table
echo '</table>';
}
// Free the result
$result->free();
}
else
{
echo 'Error! SQL query failed:';
echo "<pre>{$dbLink->error}</pre>";
}
到目前为止,这是我得到的
使用JSON中所需的属性布局创建php数组。
json_编码$yourarraydata;
????
赢
从您所需要的JSON数据来看,这似乎是最简单的解决方案 基本上,使用stdClass创建一个新类,以满足JSON数据是对象的需求。 然后更改while循环,将结果行作为对象而不是assoc数组返回,然后可以将其放入新类的images数组属性中,每返回一行对应一个属性 现在,PHP类中有了所有数据,只需使用JSON_encode将其转换为JSON字符串。结果可以作为返回的数据进行回显 我假设用于封装数据库访问代码的类有一个方法,该方法将返回对象形式的行以及assoc数组 如果您正在使用生成的json字符串执行其他操作,那么只需执行以下操作
您尝试过JSON转换吗?如果是这样,请说明您尝试了什么以及具体出了什么问题。PHP可能会有帮助。您是否尝试过json_encode check谢谢Riggs,让我尝试一下json现在正在显示,但如何让它以名称的形式显示图像的完整url:而不是名称:3fYUZH2OfZY.jpgSee我的答案了解一些其他信息。基本上,在将名称属性添加到图像数组之前,您可以修改名称属性或添加新属性,即url。
{
"success": 1,
"message": "images Available",
"images": [
{
"Name": "richmahnn",
"Mime": "ajh544k",
"Size": "222",
"Created": "232014",
"url": "http://localhost/imageUpload/ajh544k.jpg"
},
{
"Name": "john",
"Mime": "ajh5644k",
"Size": "15",
"Created": "232014",
"url": "http://localhost/imageUpload/ajh5644k.jpg"
},
// Query for a list of all existing files
$sql = 'SELECT `id`, `name`, `mime`, `size`, `created` FROM `file`';
$result = $dbLink->query($sql);
$toJson = new stdClass();
// Check if it was successfull
if($result) {
// Make sure there are some files in there
if($result->num_rows == 0) {
echo '<p>There are no files in the database</p>';
toJson->success = 0;
toJson->message = 'There are no files in the database';
} else {
// Print the top of a table
echo '<table width="100%">
<tr>
<td><b>Name</b></td>
<td><b>Mime</b></td>
<td><b>Size (bytes)</b></td>
<td><b>Created</b></td>
<td><b> </b></td>
</tr>';
// Print each file
while($row = $result->fetch_object()) {
echo "<tr>
<td>{$row->name}</td>
<td>{$row->mime}</td>
<td>{$row->size}</td>
<td>{$row->created}</td>
<td><a href='get_file.php?id={$row->id}'>Download</a></td>
</tr>";
// amend and existing properties
$row->name = 'http://'
. $_SERVER['HTTP_HOST']
. '/imageUpload/'
. $row->name;
// or alternatively add a new property
$row->url = 'http://'
. $_SERVER['HTTP_HOST']
. '/imageUpload/'
. $row->name;
$toJson->images[] = $row;
}
// Close table
echo '</table>';
$toJson->message = 'images Available';
$toJson->imageCount = $result->num_rows;
}
// Free the result
$result->free();
} else {
toJson->success = 0;
toJson->message ='Error! SQL query failed:';
echo 'Error! SQL query failed:';
echo "<pre>{$dbLink->error}</pre>";
}
echo json_encode($toJson);
exit;
$json_string = json_encode($toJson);