2016-10-15 95 views
2

我有以下以List的形式從數據庫返回的String,我的假設是該列表包含3個項目。 但它只顯示「1」的大小。所以它將所有的活動項目作爲一個元素返回。將一個JSONAray項目轉換爲多個項目

注意:當我嘗試獲取列表的第一個索引(list.get(0))時,它只返回一個「活動」不是全部三個(作爲單個項目)。我不知道是什麼是 發生在列表中。

我的問題:如何將包含1個項目(帶有3個活動項目)的字符串的以下列表轉換爲應該考慮列表大小爲3(3個活動)的列表。

[ { "activity" : { "id" : "a_3" , "kind" : "Infomation" , "userId" : 100 , "accountId" : 0 , "timestamp" : 1476369009366 , "result" : "normal" }} , 
{ "activity" : { "id" : "a_18" , "kind" : "Infomation" , "userId" : 100 , "accountId" : 0 ,"timestamp" : 1476419696003 , "result" : "normal" }} , 
{ "activity" : { "id" : "a_4" , "kind" : "Infomation" , "userId" : 100, "accountId" : 0 , ""timestamp" : 1476435910335 , "result" : "normal" }}] 

以上信息是從DB:

Iterable<DBObject> results =null; 
List<String> retList = new ArrayList<>(); 
     try { 
      results= collection.aggregate(pipeline).results(); 
     } catch (Exception e) { 
     } 
     if(results!=null){ 
      Iterator<DBObject> iterator = results.iterator(); 
      while (iterator.hasNext()) { 
       String input = iterator.next().get("actLogList").toString(); 
       retList.add(input.substring(input.indexOf("[") + 1, input.lastIndexOf("]"))); 
      } 
     } 
     return retList; 
+1

這是JSON,你需要分析你的模型類的列表 –

+0

您的列表中包含3項'activity'和每個項目還包含你的單身'項目object' –

+0

@PavneetSingh,是的我的清單包含4個項目,但它顯示大小爲1. –

回答

0

你必須使用GSON庫來解析這個JSON數組。你需要做類似於下面的代碼。

JSONArray jsonArray = new JSONArray(jsonArrayString); 
    List<String> list = new ArrayList<String>(); 
    for (int i=0; i<jsonArray.length(); i++) { 
     list.add(jsonArray.getString(i)); 
    } 
1

1)通過使你的字符串創建json陣列以構造器

2.)橫移陣列和使用索引i

3.檢索您jsonObject)獲取activityjsonobject,然後簡單地使用您的索引獲取jsonActivityItem對象的值。

4)創建一個POJO類的對象存儲像列表等集合中,但要確保將它們標記private,並使用getter和setter方法最佳實踐

class User{ 
    String id,kind,result; 
    int userId,accountId; 
    long timestamp; 
    //.. getter & setters 
} 

注:要將字符串轉換爲JSON兼容,你可以使用JsonParser,請訪問這個鏈接JsonParser snippet

JSONArray array; 
    List<User> listUser=new ArrayList<>(); // to store objects as details 
    try { 
     array=new JSONArray(st); // st is your string (make sure to use jsonparser) 
     JSONObject jsonActivity; 
     JSONObject jsonActivityItem; 
     User user; 
     for (int i=0;i<array.length();i++) { 
      System.out.println(); 
      jsonActivity=array.getJSONObject(i); 
      jsonActivityItem=jsonActivity.getJSONObject("activity");     

  // To store data for later use 
      user = new User(); 
      user.setId(jsonActivityItem.getString("id")); 
      user.settimestamp(jsonActivityItem.getLong("timestamp")); 
      //..set other values using setters 
      listUser.add(user); 

  // to display items 

      String id  = jsonActivityItem.optString("id"); 
      String kind  = jsonActivityItem.optString("kind"); 
      String userId = jsonActivityItem.optString("userId"); 
      String accountId = jsonActivityItem.optString("accountId"); 
      String timestamp = jsonActivityItem.optString("timestamp"); 
      String result = jsonActivityItem.optString("result"); 
     } 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

我會建議一個簡單而簡短的方法我。e。使用gson將其轉換成列表,但你會需要使用POJO類,你可以在this link看一看創建一個兼容的POJO類,後來只使用Gson code

婚姻無效檢查:有時你可能會看到一些例外的時候該值不適用,因此在這種情況下,當你不能確定的價值 存在這樣使用optIntoptBoolean等,這將直接返回默認值,如果它不存在,甚至嘗試值轉換爲int如果是string

docs

獲取與關鍵,或默認相關的可選int值,如果 不存在這樣的鍵,或者該值不是一個數字。如果值爲 一個字符串,則會嘗試將其評估爲數字。

int block_id = jObj.getInt("key",defaultvalue); 

int block_id = jObj.getInt("userId",0); 
+0

如果您有控制在這個數據庫輸出,然後我會建議讓你的'活動'索引一個單一的數組,包含所有其他對象這樣會更有效率,而不是包含單個-2對象的3個對象 –