我开始学习json并开发一个测验应用程序,它从php页面获取json以将其显示为html。我用php从mysql表中选择数据并将其打印为json。
<?php
header('Access-Control-Allow-Origin: *');
header("Cache-Control: no-cache");
header('Content-Type: application/json');
include("dbcon.php");
$sql = "SELECT * FROM quiz;";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo json_encode($row, JSON_PRETTY_PRINT)."\r\n";
}
}
else {
echo "nodata";
}
$conn->close();
?>
结果是:
{
"id": "1",
"question": "ques1?",
"opt1": "Microsoft",
"opt2": "W3C",
"opt3": "Google",
"opt4": "IBM",
"answer": "W3C"
}
{
"id": "2",
"question": "ques2?",
"opt1": "Yahoo",
"opt2": "Google",
"opt3": "Bing",
"opt4": "DuckDuckGo",
"answer": "Google"
}
这段代码完美无缺,打印json。但是如何打印相同的键值作为数组,所以输出就像 -
{
"id":[ "1", "2" ],
"question":[ "ques1?", "ques2?" ],
"opt1":[ "asd", "fgh" ],
"opt2":[ "qwe", "rty" ],
"opt3":[ "qwer", "vbbn" ],
"opt4":[ "asdfg", "ascvb" ],
"answer":[ "asd", "fgh" ],
}
我相信我期望的结果会让使用javascript从中读取数据很简单: -
var q1 = myObj.question[0];
我看到许多问题都有相同的标题,但它们都是不同的情况。如果我错了,请提供解决方案并纠正我的愚蠢。
答案 0 :(得分:1)
要获得有效的JSON,您需要执行以下操作:
header('Access-Control-Allow-Origin: *');
header("Cache-Control: no-cache");
header('Content-Type: application/json');
include("dbcon.php");
$sql = "SELECT * FROM quiz;";
$result = $conn->query($sql);
$theResultIWant = [];
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$theResultIWant[] = $row;
}
}
else {
echo "nodata";
}
$conn->close();
echo json_encode($theResultIWant, JSON_PRETTY_PRINT);
//That is all
要获得您想要的结果:
header('Access-Control-Allow-Origin: *');
header("Cache-Control: no-cache");
header('Content-Type: application/json');
include("dbcon.php");
$sql = "SELECT * FROM quiz;";
$result = $conn->query($sql);
$theResultIWant = [];
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
foreach ($row as $key=>$value) {
$theResultIWant[$key] = $theResultIWant[$key]??[]; //or isset($theResultIWant[$key])?$theResultIWant[$key]:[]; in PHP < 7
$theResultIWant[$key][] = $value;
}
}
}
else {
echo "nodata";
}
$conn->close();
echo json_encode($theResultIWant, JSON_PRETTY_PRINT);
任何一种方法都可以合理地阅读JavaScript,但第二种方法在大小方面被认为更紧凑(但是如果你对结果进行GZipping,第一种方法可以被压缩得更多)。
答案 1 :(得分:1)
在while循环之前为对象定义一个数组,例如
$json=array();
在while循环中执行以下操作
$json['object-key'][]=$row['key'];
示例
$json['id'][]=$row['id'];
while循环之后使用json_encode()将$ json数组转换为json对象;功能
答案 2 :(得分:1)
<?php
header('Access-Control-Allow-Origin: *');
header("Cache-Control: no-cache");
header('Content-Type: application/json');
include("dbcon.php");
$sql = "SELECT * FROM quiz;";
$result = $conn->query($sql);
$dataArr = [];
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$dataArr['id'][] = $row['id'];
$dataArr['question'][] = $row['question'];
$dataArr['opt1'][] = $row['opt1'];
$dataArr['opt2'][] = $row['opt2'];
$dataArr['opt3'][] = $row['opt3'];
$dataArr['opt4'][] = $row['opt4'];
$dataArr['answer'][] = $row['answer'];
}
}
else {
echo "nodata";
}
$conn->close();
?>
echo json_encode($dataArr, JSON_PRETTY_PRINT);
您可以按照我上面的编码使用。