将ResultSet转换为Java POJO

huangapple 未分类评论44阅读模式
英文:

ResultSet as ***String*** to Java POJO

问题

我有以下以字符串格式表示的ResultSet。出于与我的问题无关的原因,我只能将它作为一个字符串。如何从这个字符串创建一个Java POJO呢?

{
  "columnNames": [
    "cc.expiryDate",
    "cc.number"
  ],
  "values": [
    [
      1586131200000,
      "3529-2930-4919-1213"
    ]
  ]
}

这是我的POJO类:

public class CreditCard {
    
    private String number;
    private Date expiryDate;
    
    public Date getExpiryDate() {
        return this.expiryDate;
    }
    
    public void setExpiryDate(Date expiryDate) {
        this.expiryDate = expiryDate;
    }
    
    public String getNumber() {
        return this.number;
    }
    
    public void setNumber(String number) {
        this.number = number;
    }
}

我知道如果我有一个ResultSet JDBC对象,我知道如果我有一个JSON columnName: value 语法,我该怎么做,但是我无法找到如何在拥有这个字符串的情况下做到这一点。我不想开发一个使用反射的算法 - 我可以做到这一点。我希望使用已经存在的库。我猜有两个选项:1) 解析字符串以创建一个ResultSet对象;2) 将字符串转换为JSON columnName: value 格式。有什么建议吗?

英文:

I have the following ResultSet in string format. For reasons not related to my question, I can only have it as a String. How can I create a Java POJO from this string?

{
  "columnNames": [
    "cc.expiryDate",
    "cc.number"
  ],
  "values": [
    [
      1586131200000,
      "3529-2930-4919-1213"
    ]
  ]
}

Here is my POJO class:

public class CreditCard {
	
	private String number;
	private Date expiryDate;
	
	public Date getExpiryDate() {
		return this.expiryDate;
	}
	
	public void setExpiryDate(Date expiryDate) {
		this.expiryDate = expiryDate;
	}
	
	public String getNumber() {
		return this.number;
	}
	
	public void setNumber(String number) {
		this.number = number;
	}
}

I know how to do if I have a ResultSet JDBC object, I know how to do if I have a JSON columnName: value syntax but I cannot find how to do it having this string. I am not looking for developing an algorithm that uses reflection - I could do this. I am looking for using already existing libraries. I guess two options are: 1) Parse the string to create a ResultSet object or 2) Convert the string to JSON colunmName: value format. Any suggestions?

huangapple
  • 本文由 发表于 2020年4月11日 01:23:55
  • 转载请务必保留本文链接:https://java.coder-hub.com/61145406.html
匿名

发表评论

匿名网友

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

确定