Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

How to transform arraylist to json array in java

JSONObject jsonObject = new JSONObject();
    try {
 
      //Settings up array
      JSONArray jArry = new JSONArray();
    
      //this arraylist is contains of arraylist with tagIDs
      ArrayList<InventoryListItem> arr = Application.tagsReadInventory;
    
      int position = 0;
    
      //this arraylist is where i put my collected tagIDs from "Application.tagsReadInventory"
      ArrayList<String> tagIDs = new ArrayList<>();
  
      //looping to get tagIDs from "Application.tagsReadInventory" and put it in tagIDs arraylist
       for (position = 0; position < arr.size(); position++) {                        
           tagIDs.add(arr.get(position).getTagID());
       }
    
       //get each of the list inside tagIDs arraylist
       for (String tags : tagIDs) {
           JSONObject jObj = new JSONObject();
           jObj.put("epc",tags);
           jObj.put("antennaPort", 1);
           jArry.put(jObj);
       }
    
     jsonObject.put("reader_name", ReaderIP);
     jsonObject.put("mac_address", "asd");
     jsonObject.put("tag_reads", jArry);
    
  } catch (JSONException e) {
        e.printStackTrace();
  }
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #How #transform #arraylist #json #array #java
ADD COMMENT
Topic
Name
3+8 =