如何进行这种JSON响应格式化?

时间:2017-07-06 02:33:09

标签: php 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;
}

2 个答案:

答案 0 :(得分:0)

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

await

答案 1 :(得分:0)

构造一个包含所有结果的大型多维数组,然后在最后调用Configuration.UnitOfWork.Scope = TransactionScopeOption.Suppress;

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

json_encode()