如何拥有这种 JSON 响应格式?



美好的一天。我有 json 响应,我想在下面使用这种 json 格式,以便我可以解析它并将其存储到数据库中。我当前的 json 响应无效。希望你能纠正我的代码,这样我就可以有这种 JSON 响应。

预期的 JSON 响应

{
"login": {
"error": false,
"user": {
"br_code": 12,
"mem_id": 13,
"username": "novalyn",
"email": "gsac_tabaco@yahoo.com",
"created_at": "2016-07-22 09:05:21"
}
},
"accounts": {
"error": false,
"sl_summ": [{
"sl_desc": "PA : Savings Account",
"tr_date": "2015-08-17",
"actual_balance": "483.67",
"available_balance": "483.67"
},
{
"sl_desc": "PA : Savings - Cash Bond",
"tr_date": "2015-08-28",
"actual_balance": "10129.43",
"available_balance": "10129.43"
}
]
}
}

我当前的 JSON 格式

{
"error": false,
"user": {
"br_code": 12,
"mem_id": 13,
"username": "novalyn",
"email": "gsac_tabaco@yahoo.com",
"created_at": "2016-07-22 09:05:21"
}
} {
"error": false,
"sl_summ": [{
"sl_desc": "PA : Savings Account",
"tr_date": "2015-08-17",
"actual_balance": "483.67",
"available_balance": "483.67"
}, {
"sl_desc": "PA : Savings - Cash Bond",
"tr_date": "2015-08-28",
"actual_balance": "10129.43",
"available_balance": "10129.43"
}]
}

PHP代码

$response = array("error" => FALSE);
$sl_response["error"] = FALSE;
$sl_response["sl_summ"] = array();
$user = $db->getUserByUsernameAndPassword($username, $password);
if ($user != null) {
// user is found
$response["error"] = FALSE;
$response["user"]["br_code"] = $user["MEMBER_ID_BRCODE"];
$response["user"]["mem_id"] = $user["MEMBER_ID"];
$response["user"]["username"] = $user["USERNAME"];
$response["user"]["email"] = $user["EMAIL"];
$response["user"]["created_at"] = $user["REG_DATE"];
json_encode($response, true);
//Displaying json value
echo json_encode($response, true);
// FOR SL SUMM
$user_sldtl = $db->getUserSLsummary($arclass, $loanclass, $accintreceivable, $date, $year, $month, $br_code, $clientid);
If($user_sldtl != null) {
for($i = 0; $i < count($user_sldtl); $i++){
$item = array();
$item["sl_desc"] = $user_sldtl[$i][7];
$item["tr_date"] = $user_sldtl[$i][10];
$item["actual_balance"] = $user_sldtl[$i][14];
$item["available_balance"] = $user_sldtl[$i][14];
$response = array("error" => FALSE);
$sl_response["sl_summ"][] = $item;
}
json_encode($sl_response);
echo json_encode($sl_response, true);
}
else {
$sl_response["error"] = TRUE;
$sl_response["error_msg"] = "NO SL Details found!";
echo json_encode($sl_response);
}
}
else {
// user is not found with the credentials
$response["error"] = TRUE;
$response["error_msg"] = "Login credentials are wrong. Please try again!";
json_encode($response);
echo json_encode($response);
// echo "<br />" .$username. "<br />";
// echo $password;
}

构造一个包含所有结果的大型多维数组,然后在最后调用echo json_encode()

另外,我不确定为什么你把json_encode($variable);放在每echo json_encode($variable);行之前——json_encode()没有任何副作用,它只是返回编码的字符串。

<?php
$login_response = array();
$user = $db->getUserByUsernameAndPassword($username, $password);
if ($user != null) {
// user is found
$login_response["error"] = FALSE;
$login_response["user"]["br_code"] = $user["MEMBER_ID_BRCODE"];
$login_response["user"]["mem_id"] = $user["MEMBER_ID"];
$login_response["user"]["username"] = $user["USERNAME"];
$login_response["user"]["email"] = $user["EMAIL"];
$login_response["user"]["created_at"] = $user["REG_DATE"];
// FOR SL SUMM
$user_sldtl = $db->getUserSLsummary($arclass, $loanclass, $accintreceivable, $date, $year, $month, $br_code, $clientid);
$sl_response = array();
If($user_sldtl != null) {
$sl_response["error"] = FALSE;
$sl_response["sl_summ"] = array();
foreach ($user_sldtl as $dtl)){
$item = array();
$item["sl_desc"] = $dtl[7];
$item["tr_date"] = $dtl[10];
$item["actual_balance"] = $dtl[14];
$item["available_balance"] = $dtl[14];
$sl_response["sl_summ"][] = $item;
}
}
else {
$sl_response["error"] = TRUE;
$sl_response["error_msg"] = "NO SL Details found!";
}
$response = array("login" => $login_response, "accounts" => $sl_response);
}
else {
// user is not found with the credentials
$login_response["error"] = TRUE;
$login_response["error_msg"] = "Login credentials are wrong. Please try again!";
$response = array("login" => $login_response);
}
echo json_encode($response);

我只是介绍了良好的路径用例,它将生成预期的响应作为示例 JSON 有效负载。您需要为所有错误场景添加类似的结构。

$response = array("error" => FALSE);
$sl_response["error"] = FALSE;
$sl_response["sl_summ"] = array();
$user = $db->getUserByUsernameAndPassword($username, $password);
if ($user != null) {
// user is found
$response["login"]["error"] = FALSE;
$response["login"]["user"]["br_code"] = $user["MEMBER_ID_BRCODE"];
$response["login"]["user"]["mem_id"] = $user["MEMBER_ID"];
$response["login"]["user"]["username"] = $user["USERNAME"];
$response["login"]["user"]["email"] = $user["EMAIL"];
$response["login"]["user"]["created_at"] = $user["REG_DATE"];
json_encode($response, true);
//Displaying json value
echo json_encode($response, true);
// FOR SL SUMM
$user_sldtl = $db->getUserSLsummary($arclass, $loanclass, $accintreceivable, $date, $year, $month, $br_code, $clientid);
If($user_sldtl != null) {
for($i = 0; $i < count($user_sldtl); $i++){
$item = array();
$item["sl_desc"] = $user_sldtl[$i][7];
$item["tr_date"] = $user_sldtl[$i][10];
$item["actual_balance"] = $user_sldtl[$i][14];
$item["available_balance"] = $user_sldtl[$i][14];
$sl_response["sl_summ"][] = $item;
}
$response["accounts"] = $sl_response;
json_encode(response);
echo json_encode($response, true);
}
else {
$sl_response["error"] = TRUE;
$sl_response["error_msg"] = "NO SL Details found!";
echo json_encode($sl_response);
}
}
else {
// user is not found with the credentials
$response["error"] = TRUE;
$response["error_msg"] = "Login credentials are wrong. Please try again!";
json_encode($response);
echo json_encode($response);
// echo "<br />" .$username. "<br />";
// echo $password;
}

最新更新