2017-06-21 58 views
0

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

​​
+0

對不起,沒有明白你的問題,你可以請詳細說明嗎? – sumit

+0

當我登錄時,它會重試一個Json錯誤:沒有值爲sl_summ。即使它在sl_summ中有一個值。 – John

+0

你能分享代碼你如何在登錄時獲取json? – sumit

回答

0

至於你說的你已經解析了用戶數據。

對於sl_summ使用本:

我已經採取了你的反應中就可以直接做它從你的迴應字符串:

private static String sl_summResponse = "{\n" + 
      " \"error\": false,\n" + 
      " \"sl_summ\": {\n" + 
      " \"sl_brcode\": \"12\",\n" + 
      " \"sl_memid\": \"13\",\n" + 
      " \"sl_desc\": \"PA : Savings Account\",\n" + 
      " \"tr_date\": \"2015-08-17\",\n" + 
      " \"actual_balance\": \"483.67\",\n" + 
      " \"available_balance\": \"483.67\"\n" + 
      " }\n" + 
      "},\n" + 
      "{\n" + 
      " \"error\": false,\n" + 
      " \"sl_summ\": {\n" + 
      " \"sl_brcode\": \"12\",\n" + 
      " \"sl_memid\": \"13\",\n" + 
      " \"sl_desc\": \"PA : Savings - Cash Bond\",\n" + 
      " \"tr_date\": \"2015-08-28\",\n" + 
      " \"actual_balance\": \"10129.43\",\n" + 
      " \"available_balance\": \"10129.43\"\n" + 
      " }\n" + 
      "}"; 

然後解析你這樣的迴應:

try { 
      JSONObject jObj = new JSONObject(sl_summResponse); 
      Boolean error = jObj.getBoolean("error"); 
      Log.e("Test","Error : "+error); 
      if (!error){ 
       JSONArray sl_summ = jObj.optJSONArray("sl_summ"); 
       for (int i =0;i<sl_summ.length();i++){ 
        JSONObject rootObj = sl_summ.getJSONObject(i); 
        String sl_brcode = rootObj.getString("sl_brcode"); 
        String sl_memid = rootObj.getString("sl_memid"); 
        String sl_desc = rootObj.getString("sl_desc"); 
        String tr_date = rootObj.getString("tr_date"); 
        String actual_balance = rootObj.getString("actual_balance"); 
        String avail_balance = rootObj.getString("avail_balance"); 
        // then do whatever you want with this data 
        // Inserting row in users table 
        db.addUserSLDTL(sl_brcode, sl_memid, sl_desc, tr_date, actual_balance, avail_balance); 
       } 
      } 
+0

但先生我不知道私人靜態字符串sl_summResponse的確切值。因爲數據是動態的,它會依賴會是怎樣的表「SL」內部數據」 – John

+0

一些用戶SL表內的1個數據有一些2個或更多...我怎樣才能使它的動態先生? – John

+0

Np個什麼價值是我採取它是否有一個值20也不會有所作爲的陣列,它已經是dyanmic – sumit

相關問題