Home > database >  How to put my collected list (ArrayList) to JSON Array in Java?
How to put my collected list (ArrayList) to JSON Array in Java?

Time:09-18

I want to put my collected tagIDs to epc (json object inside of my array) and set a default antenna port value as "1" but everytime my loop is running all tagIDs is stuck on one line.

This is my code so far.

JSONObject jsonObject = new JSONObject();
    try {
        //Settings up array
        JSONObject jObj = new JSONObject();
        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
        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());
            jObj.put("epc",tagIDs);
            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();
}

and i want a json format like this.

{
  "reader_name": "192.168.1.332",
  "mac_address": "asd",
  "tag_reads": [
        {
        "epc": "474103534923303031343431",
        "antennaPort": 1
        },
        {
        "epc": "474103534923303031333232",
        "antennaPort": 1
        },
        {
        "epc": "47410353492330303035303D",
        "antennaPort": 1
        }
    ]
}

but this is what my result looks like.

{
  "reader_name": "192.168.1.44",
  "mac_address": "asd",
  "tag_reads": [
        {
        "epc": "474103534923303031343431","474103534923303031343431","474103534923303031343431"
        "antennaPort": 1
        }
    ]
}

CodePudding user response:

Currently you are creating a single instance of JSONObject and placing it into array. Instead of ther you should re-create your jObj every iteration:

for (position = 0; position < arr.size(); position  ) {
            JSONObject jObj = new JSONObject();
            tagIDs.add(arr.get(position).getTagID());
            jObj.put("epc",tagIDs);
            jObj.put("antennaPort", 1);
            jArry.put(jObj);
         }
    ```
   

CodePudding user response:

I already did it buy adding a new foreach for getting each of the list of my tagIDs arraylist, and it works perfectly.

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