Convert a single JSONAray to multiple elements

I have the following row returning from a database as a list. My guess is that the list contains 3 items. but it only shows "1" as the size. Therefore, it returns all elements of the activity as one element.

Note. When I try to get the first index of the list (list.get (0)), it returns only one "asset", not all three (as one item). I do not know what is happening inside the list.

My question is: how to convert the list of strings below containing 1 element (with 3 activity elements) to a list that should take the list size into account as 3 (3 activities).

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

The above information from the database:

 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; 
+2
java arraylist
Oct. 15 '16 at 6:41
source share
2 answers

1.) create a json array by passing a string to the constructor

2.) and return your jsonObject using index i

3.) jsonObject activity jsonObject , and then just use your index to retrieve the values โ€‹โ€‹for the jsonActivityItem object.

4.) create a POJO class to store your object in a collection, such as List, etc., but be sure to mark them private and use getters and setters for best practice

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

Note. To convert your string to json compatible, you can use JsonParser , try this link fragment JsonParser

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

I would recommend a simple and short way, i.e. using gson to convert it to a list, but you will need to use the POJO class, you can take a look at this link to create a compatible POJO class, and then just use the Gson code

Validation check . Sometimes you can see some exceptions when the values โ€‹โ€‹are not available, so in cases where you are not sure about the value, use optInt optBoolean , etc., which will simply return the default value if it is missing, and even try to convert the value to int if it is string like

from docs

Get the optional int value associated with the key, or the default value if there is no such key or if the value is not a number. If the value is a string, an attempt will be made to evaluate it as a number.

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

eg

 int block_id = jObj.getInt("userId",0); 
+1
Oct 15 '16 at 9:58
source share

You need to use the GSON library to parse this JSON array. You need to do something similar to the code below.

  JSONArray jsonArray = new JSONArray(jsonArrayString); List<String> list = new ArrayList<String>(); for (int i=0; i<jsonArray.length(); i++) { list.add( jsonArray.getString(i) ); } 
0
Oct 15 '16 at 6:49
source share



All Articles