Php 如何设置这种JSON响应格式?

Php 如何设置这种JSON响应格式?,php,json,Php,Json,你好。我有json响应,我希望下面有这个json格式,这样我就可以解析它并将它存储到数据库中。我当前的json响应无效。希望您能更正我的代码,这样我就可以得到这种json响应 预期的JSON响应 { "login": { "error": false, "user": { "br_code": 12, "mem_id": 13, "username": "n

你好。我有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;
}
$response=array(“错误”=>FALSE);
$sl_响应[“错误”]=FALSE;
$sl_response[“sl_summ”]=array();
$user=$db->getUserByUsernameAndPassword($username,$password);
如果($user!=null){
//找到用户
$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);
//显示json值
echo json_encode($response,true);
//对于SL SUMM
$user\u sldtl=$db->getUserSLsummary($arclass、$loanclass、$accintrevable、$date、$year、$month、$br\u code、$clientid);
如果($user\u sldtl!=null){
对于($i=0;$iFALSE);
$sl_response[“sl_sum”][]=$item;
}
json_编码($sl_响应);
echo json_encode($sl_响应,true);
}
否则{
$sl_响应[“错误”]=TRUE;
$sl_response[“error_msg”]=“未找到sl详细信息!”;
echo json_编码($sl_响应);
}
}
否则{
//找不到具有凭据的用户
$response[“error”]=TRUE;
$response[“error_msg”]=“登录凭据错误。请重试!”;
json_编码($response);
echo json_编码($response);
//回显“
.”$username.“
”; //echo$密码; }
我只是介绍GoodPath用例,它将生成预期的响应作为示例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;
}
$response=array(“错误”=>FALSE);
$sl_响应[“错误”]=FALSE;
$sl_response[“sl_summ”]=array();
$user=$db->getUserByUsernameAndPassword($username,$password);
如果($user!=null){
//找到用户
$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);
//显示json值
echo json_encode($response,true);
//对于SL SUMM
$user\u sldtl=$db->getUserSLsummary($arclass、$loanclass、$accintrevable、$date、$year、$month、$br\u code、$clientid);
如果($user\u sldtl!=null){
对于($i=0;$i.”$username.“
”; //echo$密码; }
构造一个包含所有结果的大型多维数组,然后在最后调用
echo json_encode()

另外,我不知道为什么要把
json_encode($variable)在每个
echo json_encode($variable)之前行--
json\u 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);

不能在同一输出中使用
echo json\u encode()
两次。您必须将要返回的所有内容组合到一个数组中,然后调用
json\u encode()
。先生,我该怎么做?你能给我一个例子吗,先生@Barmar?把所有的东西都放到
$response
数组中,然后调用
echo-json\u-encode($response)在最后。先生,您能给出一个示例代码吗?我试图得到这样的回应,但我没有得到那样的回应。。很抱歉问这样的问题,先生。这和原来的问题一样。每次通过循环,您都在执行
echo json\u encode($sl\u response)