如何具有这种JSON响应格式? [英] How to have this kind of JSON Response formatting?

查看:87
本文介绍了如何具有这种JSON响应格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天.我有json响应,我想在下面使用此json格式,以便我可以解析它并将其存储到数据库中.我当前的json响应无效.希望您可以更正我的代码,以便我可以收到这种json响应.

Good day. I have json response and I want to have this json format below so that I can parse it and stored it into a database. My current json response is invalid. hope you can correct my code so that I can have this kind of json response.

期望的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().

Construct one big multi-dimensional array containing all the results, then call echo json_encode() at the very end.

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

Also, I'm not sure why you put json_encode($variable); before each echo json_encode($variable); line -- json_encode() doesn't have any side effects, it just returns the encoded string.

<?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响应格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆