簡體   English   中英

使用來自字符串值的大量變量初始化 object

[英]Initializing an object with a lot of variables from String values

我是一家公司的 Web 應用程序,您可以在其中上傳 Excel 文件並將其中一張表格轉換為 JSON 文本。 為此,我使用 class,其中每個 object 代表所述工作表中的一行,其中 object 的每個變量是一列。 這是一個包含很多列的大型 Excel 文件,因此存在很多變量。

public class MetaDataRow {

private String deleted = "";
private String bo_name = "";
private String typeKey = "";
private String category = "";
private String descriptor = "";
private String group_name = "";
private boolean Zusatzobjekt = false;
private String group_layout = "";
private boolean collapsed = false;
private boolean visibleInCreationMode = true;
private int group_gridSize = 12;
private int groupPosition = 1;
private int maxCount = 1;
private String fachID = "";
private String name = "";
private String attributeName = "";
private String format = "";
private String fieldType = "";
private String dependentField = "";
private String controllLogicOfDependentField = "";
private String dependentValidationFormat = "";
private String length = "";
private String rangeMin = "";
private String rangeMax = "";
private String dateRangeMin = "";
private String possibleValues = "";
private String werteliste = "";
private String defualtValue = "";
private String fachbereiche = "";
private boolean visible = true;
private boolean editable = true;
private boolean pflichtfeld = true;
private boolean genehmigungspflichtig = true;
private String tooltip = "";
private boolean filterAttribute = true;
private boolean showInSearchResults = true;
private int position = 1;

問題是 Excel 文件在工作表中的列順序並不總是相同,所以我必須讀出列名並一一初始化字段。 執行此操作的有效方法是什么? 是否可以制作某種“General-Setter”,您可以在其中根據字符串選擇要設置的變量,例如

public void setter(String fieldName, Object content) { Variable with name fieldname = content; }

如果這不起作用,您有什么建議?

用我發現的 General Setter 解決

        private void callSetter(Object obj, String fieldName, Object value){
          PropertyDescriptor pd;
          try {
           pd = new PropertyDescriptor(fieldName, obj.getClass());
           pd.getWriteMethod().invoke(obj, value);
          } catch (IntrospectionException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
          }
         }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM