Json的錯誤:sl_summ如何修復X沒有值以JSON
PHP代碼
If($user_sldtl != null) {
for($i = 0; $i < count($user_sldtl); $i++){
$sl_response["error"] = FALSE;
$sl_response["sl_summ"]["sl_brcode"] = $user_sldtl[$i][0];
$sl_response["sl_summ"]["sl_memid"] = $user_sldtl[$i][3];
$sl_response["sl_summ"]["sl_desc"] = $user_sldtl[$i][7];
$sl_response["sl_summ"]["tr_date"] = $user_sldtl[$i][10];
$sl_response["sl_summ"]["actual_balance"] = $user_sldtl[$i][14];
$sl_response["sl_summ"]["available_balance"] = $user_sldtl[$i][14];
json_encode($sl_response, true);
echo json_encode($sl_response, true);
//echo "<br>";
}
}
else {
// echo "Member's data not found"."<br />";
// echo "Var dump: ";
// var_dump($user);
// echo " ->No record found";
$sl_response["error"] = TRUE;
$sl_response["error_msg"] = "NO SL Details found!";
echo json_encode($sl_response);
}
JSON結果用戶登錄
第一反應沒有價值。
{"error":false,"user":{"br_code":12,"mem_id":13,"username":"test","email":"[email protected]","created_at":"2016-07-22 09:05:21"}}
sl_summ的第二個響應。它有兩個響應,但沒有與其他列相同的值。該sl_summ反應將取決於他有多少行有表 「SL」
{"error":false,"sl_summ":{"sl_brcode":"12","sl_memid":"13","sl_desc":"PA : Savings Account","tr_date":"2015-08-17","actual_balance":"483.67","available_balance":"483.67"}}
{"error":false,"sl_summ":{"sl_brcode":"12","sl_memid":"13","sl_desc":"PA : Savings - Cash Bond","tr_date":"2015-08-28","actual_balance":"10129.43","available_balance":"10129.43"}}
slsumm.java內
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
// Check for error node in json
if (!error) {
// user successfully logged in
// Create login session
session.setLogin(true);
// Now store the user in SQLite
//String uid = jObj.getString("uid");
JSONObject user = jObj.getJSONObject("user");
String br_code = user.getString("br_code");
String mem_id = user.getString("mem_id");
String username = user.getString("username");
String email = user.getString("email");
String created_at = user.getString("created_at");
// Inserting row in users table
db.addUser(br_code, mem_id, username, email, created_at);
JSONObject sl_summ = jObj.getJSONObject("sl_summ");
String sl_brcode = sl_summ.getString("sl_brcode");
String sl_memid = sl_summ.getString("sl_memid");
String sl_desc = sl_summ.getString("sl_desc");
String tr_date = sl_summ.getString("tr_date");
String actual_balance = sl_summ.getString("actual_balance");
String avail_balance = sl_summ.getString("avail_balance");
// Inserting row in users table
db.addUserSLDTL(sl_brcode, sl_memid, sl_desc, tr_date, actual_balance, avail_balance);
// Launch main activity
Intent intent = new Intent(LoginActivity.this,
MainActivity.class);
startActivity(intent);
finish();
} else {
// Error in login. Get the error message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
SQLiteHandler.java
對不起,沒有明白你的問題,你可以請詳細說明嗎? – sumit
當我登錄時,它會重試一個Json錯誤:沒有值爲sl_summ。即使它在sl_summ中有一個值。 – John
你能分享代碼你如何在登錄時獲取json? – sumit