用字符串值初始化一个包含许多变量的对象

huangapple go评论85阅读模式
英文:

Initializing an object with a lot of variables from String values

问题

我正在制作一个公司的 Web 应用程序,在这个应用程序中,您可以上传一个 Excel 文件,并将其中的一个工作表转换为 JSON 文本。为此,我使用了一个类,其中每个对象表示工作表中的一行,对象的每个变量是一列。这是一个包含许多列的大型 Excel 文件,因此有很多变量。

以下是您提供的类的部分代码:

public class MetaDataRow {

    private String deleted = "";
    private String bo_name = "";
    private String typeKey = "";
    // ... 其他变量 ...
    private boolean showInSearchResults = true;
    private int position = 1;

    // 构造函数和其他方法(未提供)

}

问题在于 Excel 文件中的列顺序并不总是相同的,所以我必须读取列的名称,并逐个初始化字段。有什么高效的方法可以做到这一点吗?也许可以制作一种类似于“通用设置器”的东西,您可以根据字符串选择要设置的变量,例如:

public void setter(String fieldName, Object content) {
    if ("deleted".equals(fieldName)) {
        deleted = (String) content;
    } else if ("bo_name".equals(fieldName)) {
        bo_name = (String) content;
    } else if ("typeKey".equals(fieldName)) {
        typeKey = (String) content;
    }
    // ... 其他变量 ...
}

如果这种方法不起作用,您有什么建议?

(请注意,由于您要求只返回翻译好的部分,我已经省略了一些代码和上下文,以便只提供核心内容。如果您需要更多信息,请随时告诉我。)

英文:

I am makung a company Web-Application where you can upload an Excel-File and convert one of its Sheets into a JSON text. For this I use a class where each object represents one row from said sheet, where each variable of the object is one column. It's a large Excel-File with a lot of Columns, so there a lot of variables.

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;

The problem is that the Excel Files do not always have the same order of columns in the sheet, so I have to read out the names of the columns and initialize the fields one by one.
What would be an efficient way to do this? Would it maybe be possible to make some sort of "General-Setter" where you can choose which variable to set based on a String, e.g.

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

If that doesn't work, what are your suggestions?

答案1

得分: 0

使用我找到的通用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();
    }
}
英文:

Solved with a General Setter I found

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

huangapple
  • 本文由 发表于 2020年10月9日 16:32:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64276546.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定