简体   繁体   English

如何将我收集的列表(ArrayList)放入Java中的JSON数组中?

[英]How to put my collected list (ArrayList) to JSON Array in Java?

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.我想将收集到的 tagID 放入 epc(我的阵列中的 json object)并将默认天线端口值设置为“1”,但每次我的循环运行时,所有 tagID 都会卡在一行上。

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.我想要一个像这样的 json 格式。

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

Currently you are creating a single instance of JSONObject and placing it into array.目前,您正在创建 JSONObject 的单个实例并将其放入数组中。 Instead of ther you should re-create your jObj every iteration:而不是你应该重新创建你的 jObj 每次迭代:

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

I already did it buy adding a new foreach for getting each of the list of my tagIDs arraylist, and it works perfectly.我已经购买了添加一个新的 foreach 来获取我的每个 tagID arraylist 列表,并且效果很好。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM