我在对象内有一个数组,我想向该数组添加多个值,但是我的代码开始将它们分开。响应应如下所示:
{
"requestTime": "1",
"clients": [{
"name": "Peter",
"id": 905
}]
}
但与此相反,它看起来像这样:
{
"requestTime": "1",
"clients": [{
"name": "Peter"
}, {
"id": 905
}]
}
我的代码:
$myObj = new stdClass();
$myObj->requestTime = $reqtime;
$myObj->clients[]->id = $id;
$myObj->clients[]->name = $name;
$myJSON = json_encode($myObj);
echo $myJSON;
答案 0 :(得分:2)
一次性构建数组,而不是分两步生成两个数组。
<!Doctype html>
<html>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "daylight";
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$startdate=time()-(time()%(24*3600))-7200;
$enddate=$startdate+365*24*3600;
//echo $startdate."<br>".$enddate;
$s=strftime("%Y-%m-%d", $startdate);
$e=strftime("%Y-%m-%d", $enddate);
$Off = 12; //Offset
$A = 4; //Amplidute
$light=0;
for($i=$startdate; $i<=$enddate; $i+=$day) //genData (works)
{...}
$sql_request = "SELECT * FROM `db` WHERE `Datum` >= '$s' And `Datum` <= '$e'";
$daylight_array=array();
$date_array=array();
foreach($conn->query($sql_request) as $row)
{
$daylight_array[]=$row["Tageslicht"];
$date_array[]=$row["Datum"];
//print $row["Tageslicht"];
}
// echo json_encode($date_array);
// echo json_encode($daylight_array);
?>
<div id="chart" style="width: 900px; height: 500px"></div>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
var date = <?php echo json_encode($date_array); ?>;
var daylight = <?php echo json_encode($daylight_array); ?>;
function drawChart() {
var data = google.visualization.arrayToDataTable();
data.addColumn('string', 'Jahr-Monat-Tag');
data.addColumn('number', 'Sonnenstunden');
for(i = 0; i < date.length; i++)
data.addRow([date[i], daylight[i]]);
var options = {
title: 'Sonnenstunden über das Jahr',
curveType: 'function',
legend: { position: 'bottom' }
};
var chart = new google.visualization.LineChart(document.getElementById('chart'));
chart.draw(data, options);
}
</script>
</body>
</html>
答案 1 :(得分:1)
尝试做这样的事情:
enterBut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
switch(click)
{
case 1:
double subtotal = Double.parseDouble(Atext.getText());
double total = 0.05*Math.round(1.13*subtotal/0.05);
answer.setText("The total will be $" + total + " (tax included). Enter the amount of money the customer gave.");
int click = 2;
case 2:
double money = Double.parseDouble(Atext.getText());
click = 3;
if (money<total)
{
JOptionPane.showMessageDialog(window, "That is not enough money.");
click = 2;
}
}
}
});
答案 2 :(得分:0)
如果我根据您的要求输出了解了您的要求,那么这将对您有效,请使用变量代替静态id
,name
和{{1} }我使用的变量。
requestTime
输出:
<?php
$myObj = new stdClass();
$myObj->requestTime = 1;
$myObj->clients[] = ['id' => 905, 'name' => 'Peter'];
$myJSON = json_encode($myObj);
echo $myJSON;
?>