将字符串解析为Java对象

时间:2017-03-10 20:26:56

标签: java sql arrays jdbc amazon-athena

我需要将此格式的String转换为Array个对象。

[{name=Nancy Chapman, email=nchapman0@comcast.net}, {name=Jimmy Fisher, email=jfisher1@photobucket.com}]

有没有简单的方法来转换它而不必完全手动完成?

更新: 我从自定义SQL数据库(Amazon Athena)中提取这些值。自定义JDBC不支持getArray(),因此我需要手动解析包含Array Structs的列。不幸的是,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)

Java对象

MessageObj

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

RecipientObj

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.
}

1 个答案:

答案 0 :(得分:0)

也许我误解了你的问题,但如果你把它清理一下,你输入的内容应该有效,看起来像这样:

[{ name:"Nany Chapman", email:"nchapman0@comcast.net"}, 
{ name:"Nany Chapman", email:"nchapman0@comcast.net"}]

不要使用等号,而是使用冒号并在值周围加上引号。