Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

Expected JSON response

  {
      "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"
              }
          ]
      }
  }

My current JSON format

{
    "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 code

$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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
120 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...