【发布时间】:2020-02-03 21:54:17
【问题描述】:
视图必须是这样的:
{
"app": [
{
"system": {
"base_url": "https://youtube.com",
"email_address": "facebook@gmail.com",
"privacy_policy_url": "https://googles.com",
"terms_of_use_url": "https://fasfg.com",
"splash_screen_timout": 1000,
"loading_spinner": "Wave"
}
},
{
"settings": {
"dark_mode": true,
"screen_sleep": true,
"fullscreen_mode": true,
"notification_sound_option": true,
"notification_vibration_option": true,
"download_via_wifi": true,
"device_information": true,
"other_links": true,
"danger_zone": true
}
}
但是当我尝试让它变成这样时,它会覆盖旧函数并像这样打印最后一个对象:
{
"app": [
{
"settings": {
"dark_mode": true,
"screen_sleep": true,
"fullscreen_mode": true,
"notification_sound_option": true,
"notification_vibration_option": true,
"download_via_wifi": true,
"device_information": true,
"other_links": true,
"danger_zone": true
}
}
这是我的 PHP 代码,我知道这个问题,但我无法解决它,我认为答案是附加数据,但我不知道怎么做,我搜索了答案,但我不清楚:
$fetch_system = $db->prepare("SELECT base_url, email_address,
privacy_policy_url, terms_of_use_url,
splash_screen_timout, loading_spinner
FROM configuration
WHERE secret_api_key=?");
$fetch_system->bind_param("s", $_SESSION['secret_api_key']);
$fetch_system->execute();
$rows = array();
$result = $fetch_system->get_result();
while($rows1 = $result->fetch_assoc()) {
$rows['app'] = $rows1;
}
$fetch_settings = $db->prepare("SELECT dark_mode, screen_sleep, full_screen,
notification_sound, notification_vibration,
download_via_wifi, device_information,
other_links, danger_zone
FROM configuration
WHERE secret_api_key=?");
$fetch_settings->bind_param("s", $_SESSION['secret_api_key']);
$fetch_settings->execute();
$rows['app'] = array();
$result = $fetch_settings->get_result();
while($rows2 = $result->fetch_assoc()) {
$rows['settings'] = $rows2;
}
echo json_encode($rows);
【问题讨论】:
-
{}在您的 JSON 中不平衡。你有一个未关闭的[.in 每个json。请解决此问题,以便更容易理解。 -
尝试删除第二个
$rows['app'] = array();,您正在清除之前保存的值。 -
谢谢,现在可以使用了。你拯救了我的一天:)