2017-07-04 83 views
0

好日子,如何獲取這個Json響應並將其插入到Android SQLite中。 希望你能指導我如何獲取這個JSON對象一個JSON數組對象更新:如何插入一個Json數組到JSON數組到SQLite

{ 
    "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響應裏面。第一個Json對象(用戶)成功解析並插入數據庫,但第二個Json響應(sl_summ)解析失敗並未插入。這就是我的問題。

{ 
    "error": false, 
    "user": { 
    "br_code": 12, 
    "mem_id": 13, 
    "username": "novalyn", 
    "email": "[email protected]", 
    "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" 
    } 
    ] 
} 

這是logcat的消息

D/RegisterActivity: Login Response: {"error":false,"user":{"br_code":12,"mem_id":13,"username":"novalyn","email":"[email protected]","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"}]} 
07-05 16:19:15.068 20514-20514/ph.coredev.johnjessbayutas.gsacmobileportal D/[email protected][LoginActivity]: dispatchDetachedFromWindow 
07-05 16:19:15.083 20514-20514/ph.coredev.johnjessbayutas.gsacmobileportal D/InputTransport: Input channel destroyed: fd=76 
07-05 16:19:15.084 20514-20514/ph.coredev.johnjessbayutas.gsacmobileportal D/RegisterActivity: Checking JSON Object{"error":false,"user":{"br_code":12,"mem_id":13,"username":"novalyn","email":"[email protected]","created_at":"2016-07-22 09:05:21"}} 
07-05 16:19:15.085 20514-20514/ph.coredev.johnjessbayutas.gsacmobileportal D/TEST: -error attribute    : false 
07-05 16:19:15.094 20514-20514/ph.coredev.johnjessbayutas.gsacmobileportal D/SessionManager: User login session modified! 
07-05 16:19:15.095 20514-20514/ph.coredev.johnjessbayutas.gsacmobileportal D/USER: -user object  : {"br_code":12,"mem_id":13,"username":"novalyn","email":"[email protected]","created_at":"2016-07-22 09:05:21"} 
07-05 16:19:15.095 20514-20514/ph.coredev.johnjessbayutas.gsacmobileportal D/USER: -br_code   : 12 
07-05 16:19:15.095 20514-20514/ph.coredev.johnjessbayutas.gsacmobileportal D/USER: -mem_id   : 13 
07-05 16:19:15.095 20514-20514/ph.coredev.johnjessbayutas.gsacmobileportal D/USER: -username  : novalyn 
07-05 16:19:15.095 20514-20514/ph.coredev.johnjessbayutas.gsacmobileportal D/USER: -email   : [email protected] 
07-05 16:19:15.095 20514-20514/ph.coredev.johnjessbayutas.gsacmobileportal D/USER: -created at  : 2016-07-22 09:05:21 
07-05 16:19:15.117 20514-20514/ph.coredev.johnjessbayutas.gsacmobileportal D/SQLiteHandler: New member was inserted into table members: 13 
07-05 16:19:15.117 20514-20514/ph.coredev.johnjessbayutas.gsacmobileportal D/SQLiteHandler: BR CODE: 12 
07-05 16:19:15.117 20514-20514/ph.coredev.johnjessbayutas.gsacmobileportal D/SQLiteHandler: Member ID: 13 
07-05 16:19:15.117 20514-20514/ph.coredev.johnjessbayutas.gsacmobileportal D/SQLiteHandler: Username: novalyn 
07-05 16:19:15.117 20514-20514/ph.coredev.johnjessbayutas.gsacmobileportal D/SQLiteHandler: Email: [email protected] 
07-05 16:19:15.117 20514-20514/ph.coredev.johnjessbayutas.gsacmobileportal D/SQLiteHandler: Created at: 2016-07-22 09:05:21 
07-05 16:19:15.118 20514-20514/ph.coredev.johnjessbayutas.gsacmobileportal D/sl_summ: JSON String     : {"error":false,"user":{"br_code":12,"mem_id":13,"username":"novalyn","email":"[email protected]","created_at":"2016-07-22 09:05:21"}} 
07-05 16:19:15.118 20514-20514/ph.coredev.johnjessbayutas.gsacmobileportal D/sl_summ: -error attribute    : false 
07-05 16:19:15.119 20514-20514/ph.coredev.johnjessbayutas.gsacmobileportal D/TEST: org.json.JSONException: No value for sl_summ 

我注意到,只有第一個響應返回

D/sl_summ: JSON String : {"error":false,"user":{"br_code":12,"mem_id":13,"username":"novalyn","email":"[email protected]","created_at":"2016-07-22 09:05:21"}} 
D/sl_summ: -error attribute    : false 
D/TEST: org.json.JSONException: No value for sl_summ 
+0

你好,你的問題我也不清楚。你想知道如何去解析一個JSON b)在SQLite中保存JSON c)能夠從SQLite –

+0

a,b和c sir @ RuchiraRandana返回對象。我想解析JSON保存到SQLite和檢索對象 – Novice

回答

0

下面是示例代碼,JSON字符串轉換成JSON對象和訪問JSON數組及其屬性。

根據你在SQLite中的表模式,你可以在列中插入單個值或保存完整的JSON字符串,這取決於你的要求。

由於您有兩個不同的json字符串正在處理,如果您不知道哪一個先到達,那麼您可以隨時檢查JSONObject用戶或JSONArray sl_summ是否存在,並取決於您是否可以如下處理它們。請注意,這是最簡單的做法,我已經更新了代碼,只是爲了讓你去,還有很多其他的檢查方式。

import android.util.Log; 

import org.json.JSONArray; 
import org.json.JSONObject; 

public static void jsonExample() { 
    String loginString = "{\"error\":false, \"user\":{\"br_code\":12,\"mem_id\":13,\"username\":\"test\",\"email\":\"[email protected]\",\"created_at\":\"2016-07-22 09:05:21\"}}"; 
    String jsonString = "{\"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\" } ] }"; 
    try { 
     Log.d("TEST", "INPUT String     : " + jsonString); 
     JSONObject accountJSONObject = new JSONObject(jsonString); 
     displaySubAttributes(accountJSONObject); 
     Log.d("TEST", "INPUT String     : " + loginString); 
     JSONObject loginJSONObject = new JSONObject(loginString); 
     displaySubAttributes(loginJSONObject); 
    } catch (Exception exception) { 
     Log.d("TEST", exception.toString()); 
    } 
} 

public static void displaySubAttributes(JSONObject jsonObject) throws Exception { 
    // read the json string into a json object 
    Log.d("TEST", "JSON String     : " + jsonObject.toString()); 

    // access individual json object thru jsonObject.get("FIELD_NAME") 
    Log.d("TEST", "-error attribute    : " + jsonObject.get("error").toString()); 

    JSONArray slArray = jsonObject.optJSONArray("sl_summ"); 

    // Check if its login data i.e. user present 
    if (!jsonObject.isNull("user") && slArray == null) { 
     // handle user login data 
     JSONObject userJSONObject = (JSONObject) jsonObject.get("user"); 
     Log.d("TEST", "User       : " + userJSONObject.toString()); 
     Log.d("TEST", "-br_code attribute   : " + userJSONObject.get("br_code").toString()); 
     Log.d("TEST", "-mem_id attribute   : " + userJSONObject.get("mem_id").toString()); 
     Log.d("TEST", "-username attribute   : " + userJSONObject.get("username").toString()); 
     Log.d("TEST", "-email attribute    : " + userJSONObject.get("email").toString()); 
     Log.d("TEST", "-created_at attribute  : " + userJSONObject.get("created_at").toString()); 
     // Check if its account data i.e. sl_summ is present 
    } else if (slArray != null && jsonObject.isNull("user")) { 
     // handle account data 
     JSONArray array = ((JSONArray)jsonObject.getJSONArray("sl_summ")); 
     // access individual json array thru jsonObject.getJSONArray("FIELD_NAME") 
     Log.d("TEST", "-sl_summ array    : " + jsonObject.getJSONArray("sl_summ").toString()); 
     for (int index=0; index<array.length(); index++) { 
      JSONObject object = (JSONObject)array.get(index); 
      Log.d("TEST", "-sl_desc attribute   : " + object.get("sl_desc").toString()); 
      Log.d("TEST", "-tr_date attribute   : " + object.get("tr_date").toString()); 
      Log.d("TEST", "-actual_balance attribute : " + object.get("actual_balance").toString()); 
      Log.d("TEST", "-available_balance attribute : " + object.get("available_balance").toString()); 
      Log.d("TEST", "---------------------------------"); 
     } 
    } else { 
     // a new JSON string that doesn't have user or sl_summ as member variable so display it and write new handler code 
     Log.d("TEST", "Unknown JSON String   : " + jsonObject.toString()); 
    } 
} 

抽樣日誌

07-05 16:04:13.352 15643-15643/com.testing.test D/TEST: INPUT String     : {"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" } ] } 
07-05 16:04:13.352 15643-15643/com.testing.test D/TEST: JSON String     : {"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"}]} 
07-05 16:04:13.352 15643-15643/com.testing.test D/TEST: -error attribute    : false 
07-05 16:04:13.352 15643-15643/com.testing.test D/TEST: -sl_summ array    : [{"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"}] 
07-05 16:04:13.352 15643-15643/com.testing.test D/TEST: -sl_desc attribute   : PA : Savings Account 
07-05 16:04:13.352 15643-15643/com.testing.test D/TEST: -tr_date attribute   : 2015-08-17 
07-05 16:04:13.352 15643-15643/com.testing.test D/TEST: -actual_balance attribute : 483.67 
07-05 16:04:13.352 15643-15643/com.testing.test D/TEST: -available_balance attribute : 483.67 
07-05 16:04:13.352 15643-15643/com.testing.test D/TEST: --------------------------------- 
07-05 16:04:13.352 15643-15643/com.testing.test D/TEST: -sl_desc attribute   : PA : Savings - Cash Bond 
07-05 16:04:13.352 15643-15643/com.testing.test D/TEST: -tr_date attribute   : 2015-08-28 
07-05 16:04:13.352 15643-15643/com.testing.test D/TEST: -actual_balance attribute : 10129.43 
07-05 16:04:13.352 15643-15643/com.testing.test D/TEST: -available_balance attribute : 10129.43 
07-05 16:04:13.352 15643-15643/com.testing.test D/TEST: --------------------------------- 

07-05 16:04:13.352 15643-15643/com.testing.test D/TEST: INPUT String     : {"error":false, "user":{"br_code":12,"mem_id":13,"username":"test","email":"[email protected]","created_at":"2016-07-22 09:05:21"}} 
07-05 16:04:13.353 15643-15643/com.testing.test D/TEST: JSON String     : {"error":false,"user":{"br_code":12,"mem_id":13,"username":"test","email":"[email protected]","created_at":"2016-07-22 09:05:21"}} 
07-05 16:04:13.353 15643-15643/com.testing.test D/TEST: -error attribute    : false 
07-05 16:04:13.353 15643-15643/com.testing.test D/TEST: User       : {"br_code":12,"mem_id":13,"username":"test","email":"[email protected]","created_at":"2016-07-22 09:05:21"} 
07-05 16:04:13.353 15643-15643/com.testing.test D/TEST: -br_code attribute   : 12 
07-05 16:04:13.353 15643-15643/com.testing.test D/TEST: -mem_id attribute   : 13 
07-05 16:04:13.353 15643-15643/com.testing.test D/TEST: -username attribute   : test 
07-05 16:04:13.353 15643-15643/com.testing.test D/TEST: -email attribute    : [email protected] 
07-05 16:04:13.353 15643-15643/com.testing.test D/TEST: -created_at attribute  : 2016-07-22 09:05:21 
+0

先生如果我有另一個JSON之前的其他JSON響應先生。就像我登錄並驗證用戶是否存在一樣。它會返回用戶, { 「錯誤」 的JSON響應:假, 「用戶」:{ 「br_code」:12, 「mem_id」:13, 「用戶名」: 「測試」, 「電子郵件「:」test @ yahoo。com「, 」created_at「:」2016-07-22 09:05:21「 } } – Novice

+0

我會提出我的問題先生或問一個新問題,希望你能幫助我先生。 /測試:org.json.JSONException:沒有值sl_summ – Novice

+0

嗨新手,我更新了我的代碼,以便它可以處理您的兩個JSON字符串(即一個與用戶和其他與sl_summ)。你得到「沒有價值sl_summ 「,因爲你試圖用我的第一個代碼解析用戶json字符串,而第一個代碼只是構建來處理sl_summ,但現在它可以處理這兩個問題。你要注意的重要一點是你應該知道你的輸入JSON字符串,然後解析它。如果JSONObject具有用戶密鑰或sl_summ數組,然後我以不同的方式處理它們,請嘗試使用代碼並讓我知道它是否解決了您的問題並請接受答案。 – JRG

0

下面是步驟,你可以遵循:

JSONObject jsonObject = new JSONObject("Your JSON String here"); 

String error=jsonObject.getString(""); 

JSONArray s1_sum = jsonObject.getJSONArray("sl_summ"); 

int s1_sum_length = s1_sum.length(); 

for (int i = 0; i < s1_sum_length; i++) { 
String individualString = statementtag.getJSONObject(i); 
sl_desc = individualString.getString("sl_desc"); 
tr_date = individualString.getString("tr_date"); 
actual_balance = individualString.getString("actual_balance"); 
available_balance = individualString.getString("available_balance"); 

//YOU CAN WRITE INSERT QUERY HERE 
} 
0

這裏解析被調用(傳遞響應)

getParsing(response.body().string()); 

解析方法在這裏

public void getParsing(String jsonresponse) { 


     try { 
      JSONArray responseArray = new JSONArray(jsonresponse); 
      for (int i = 0; i < responseArray.length(); i++) { 
       JSONObject responseObject = responseArray.getJSONObject(i); 
       String mId = responseArray.optString("KEY_TAG to get value ID"); 
       String mName = responseArray.optString("KEY_TAG to get value Name"); 
       . 
       . 

       loginDataBaseAdapter = loginDataBaseAdapter.open(); 
       loginDataBaseAdapter.insertRole(mId, mName); 

       loginDataBaseAdapter.close(); 

      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

    } 

而且你的插入查詢像

public void insertRole(String id, String name) { 
     String selectQuery = "SELECT * FROM " + DATABASE_ROLES + " WHERE id = '" + name + "'"; 
     db = dbHelper.getWritableDatabase(); 

     Cursor cursor = db.rawQuery(selectQuery, null); 
     if (cursor.getCount() > 0) { 
      ContentValues newValues = new ContentValues(); 
      // Assign values for each row. 
      newValues.put("id", id); 
      newValues.put("name", name); 

      int rowsUpdated = db.update(DATABASE_ROLES, newValues, "id= '" + id+ "'", null); 
      Log.v("resultState", "resultState==" + rowsUpdated); 

     } else { 
      ContentValues newValues = new ContentValues(); 
      // Assign values for each row. 
      newValues.put("id", id); 
      newValues.put("name", name); 

      long result = db.insert(DATABASE_ROLES, null, newValues); 
      Log.v("resultState", "resultState==" + result); 

     } 
     cursor.close(); 

    }