【问题标题】:How to transform an ArrayList to JSON Array in Java?如何在 Java 中将 ArrayList 转换为 JSON 数组?
【发布时间】:2022-10-17 16:30:03
【问题描述】:

我想将收集到的 tagID 放入 epc(阵列内的 json 对象)并将默认天线端口值设置为“1”,但每次我的循环运行时,所有 tagID 都会卡在一行上。

到目前为止,这是我的代码。

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

我想要这样的json格式。

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

但这就是我的结果。

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

【问题讨论】:

    标签: java arrays json arraylist


    【解决方案1】:

    我已经通过添加一个新的 foreach 来获取我的 tagIDs 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();
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-12
      • 1970-01-01
      • 2020-11-15
      • 1970-01-01
      • 1970-01-01
      • 2020-05-29
      • 2020-03-24
      相关资源
      最近更新 更多