从PHP创建JSON

时间:2014-02-22 15:30:24

标签: php json

如何创建

[{ "Date" : "2013.01" , "Target" : 120 , "Reach" : 150 , "Change" : 180 } ,

{“日期”:“2013.02”,“目标”:120,“到达”:150,“更改”:180}]

由于图表需要数据,因此从

进入JSON?感谢你 由于图表需要数据,因此从此进入JSON?感谢你 由于图表需要数据,因此从此进入JSON?谢谢你

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "
http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>www</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
P { FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana, Tahoma, Arial}
TD { FONT-SIZE: 8pt; COLOR: #000000; FONT-FAMILY: Verdana, Tahoma, Arial}
-->
</style>
</head>
<body>

<?php
/*
VIEW-PAGINATED.PHP
Displays all data from 'Admin' table
This is a modified version of view.php that includes pagination
*/

// connect to the database
include('connect-db.php');
// log record
$Time = time().'.'.$extension;
$Log = 'Admin';
$IP = $_SERVER['REMOTE_ADDR'];
$query="INSERT INTO Adminlog (ID, Log, Time, IP)VALUES ('NULL', '".$Log."',
'".$Time."', '".$IP."')";
mysql_query($query) or die ('Error updating database');

print
( '<div align="left">

</div>' );

$result = mysql_query("SELECT * FROM Admin WHERE Student='1221'")
or die(mysql_error());

{
echo "<a href='rtbr.php?page=$I'>$I</a> ";
}

echo "[";
echo "<br>";
while ($row = mysql_fetch_array($result)) {
echo "<td>" . " { "
. " &quot;Date&quot; " . " : " . " &quot;2013.01&quot; " . " , "
. " &quot;Target&quot; " . " : " . $row["SubjectMEScore"] . " , "
. " &quot;Reach&quot; " . " : " . $row["SubjectEEScore"] . " , "
. " &quot;Change&quot; " . " : " . $row["SubjectCCScore"] . " } " . " , "
. "</td>";

echo "<br>";

echo "<td>" . " { "
. " &quot;Date&quot; " . " : " . " &quot;2013.02&quot; " . " , "
. " &quot;Target&quot; " . " : " . $row["SubjectMEScore"] . " , "
. " &quot;Reach&quot; " . " : " . $row["SubjectEEScore"] . " , "
. " &quot;Change&quot; " . " : " . $row["SubjectCCScore"] . " } "
. "</td>";

echo "<br>";

echo "]";

echo "</tr>";
}
// close table>
echo "</table>";

?>

</body>
</html> 

1 个答案:

答案 0 :(得分:0)

您需要做的就是将数组放在json_encode()

所以这样:

$array = array('first_value', 'second_value');
echo json_encode($array);

PHP会将数组作为JSON返回。

如果您想要解码它,就像它一样简单。使用json_decode() instaed。

echo json_decode($json_array);