将字符串解析为Java对象
我需要将此格式的将字符串解析为Java对象,java,sql,arrays,jdbc,amazon-athena,Java,Sql,Arrays,Jdbc,Amazon Athena,我需要将此格式的字符串s转换为对象的数组 [{name=Nancy Chapman, email=nchapman0@comcast.net}, {name=Jimmy Fisher, email=jfisher1@photobucket.com}] 有没有什么简单的方法可以在不需要完全手动的情况下进行转换 更新: 我从自定义SQL数据库(Amazon Athena)中提取这些值。而且自定义的JDBC不支持getArray(),因此我需要手动解析包含结构的数组的列。不幸的是,这是DB的一个限制
字符串
s转换为对象的数组
[{name=Nancy Chapman, email=nchapman0@comcast.net}, {name=Jimmy Fisher, email=jfisher1@photobucket.com}]
有没有什么简单的方法可以在不需要完全手动的情况下进行转换
更新:
我从自定义SQL数据库(Amazon Athena)中提取这些值。而且自定义的JDBC
不支持getArray()
,因此我需要手动解析包含结构的数组的列。不幸的是,这是DB的一个限制,我无法控制它。这是我在列上调用getString()
时SQL数据库返回的格式
SQL表定义
id (int)
threadid (int)
senderemail (string)
sendername (string)
subject (string)
body (string)
recipients (array<struct<name:string,email:string>>)
ccrecipients (array<struct<name:string,email:string>>)
bccrecipients (array<struct<name:string,email:string>>)
attachments (array<binary>)
date (timestamp)
RecipientObj
public class MessageObj {
private int id;
private int threadId;
private String senderEmail;
private String senderName;
private String subject;
private String body;
private List<RecipientObj> recipients;
private List<RecipientObj> ccRecipients;
private List<RecipientObj> bccRecipients;
private List<File> attachments;
private Calendar date;
}
public class RecipientObj {
private String email;
private String name;
}
解析数据
ResultSet rs = statement.executeQuery(sql);
while (rs.next()) {
// Retrieve table column.
int id = rs.getInt("id");
Integer threadId = rs.getInt("threadid");
String senderEmail = rs.getString("senderemail");
String senderName = rs.getString("sendername");
String subject = rs.getString("subject");
String body = rs.getString("body");
//How to convert recipients into ArrayList? rs.getArray("recipients") not supported.
//... Code here to add into an ArrayList of MessageObj.
}
也许我误解了你的问题,但如果你把它清理一下,使它看起来像这样,那么你键入的内容应该有效:
[{ name:"Nany Chapman", email:"nchapman0@comcast.net"},
{ name:"Nany Chapman", email:"nchapman0@comcast.net"}]
不要使用等号,而是使用冒号并在值周围加引号。为什么输入的是这种格式?他们从哪里来?它看起来有点像破损的JSON;是否涉及JSON??格式是否有名称?如果它是常见的,可能有一个库,否则您将不得不手动执行,或者使用不同的格式。为什么不遍历ResultSet来创建对象数组呢?另外,使用Hibernate之类的框架可以更容易地将值从DB映射到对象。@Alan我相信您必须手动进行数据绑定。由于Athena是一个大数据服务器,Hibernate或任何其他JPA框架都无法工作。因此,您可能需要手动执行此操作。要将其解析为JSON,键周围还必须有引号。很遗憾,我无法将输入更改为JSON。我希望我能利用像GSON这样的库