需求:
有一个已知字段名的JSON字符串数组:"{\"serverMetrics\":[{\"vip\":\"caty1.vip.ebay.com\", \"metricsName\":\"CPU Number\", \"hostName\":\"caty1\", \"metricsTime\":\"20130101 13:22:15\", \"partitionKey\":\"00\", \"value\":\"1324\", \"thresholdParam1\":\"param1\", \"thresholdParam2\":\"param2\", \"thresholdParam3\":\"param3\", \"metricsP1\":\"p1\", \"metricsP2\":\"p2\", \"metricsP3\":\"p3\"}," +
"{\"vip\":\"caty2.vip.ebay.com\", \"metricsName\":\"Memory\", \"hostName\":\"caty2\", \"metricsTime\":\"20130102 09:22:15\", \"partitionKey\":\"00\", \"value\":\"23456\", \"thresholdParam1\":\"param12\", \"thresholdParam2\":\"param22\", \"thresholdParam3\":\"param32\", \"metricsP1\":\"p12\", \"metricsP2\":\"p22\", \"metricsP3\":\"p32\"}," +
"{\"vip\":\"caty3.vip.ebay.com\", \"metricsName\":\"Storage\", \"hostName\":\"caty3\", \"metricsTime\":\"20130103 19:22:15\", \"partitionKey\":\"00\", \"value\":\"56789\", \"thresholdParam1\":param13, \"thresholdParam23\":\"param2\", \"thresholdParam33\":\"param3\", \"metricsP13\":\"p1\", \"metricsP23\":\"p2\", \"metricsP33\":\"p3\"}" "]}";
现在需要将该字符串封装到一个相应的Obejct中,考虑到Java中这类对象我们通常称为值传递对象(用来将传递的请求封装成相应的对象),我们可以创建相应的VO(Value Object), 将该VO命名为ServerMetricsVO
,而很容易看出,上面的Json格式的数据是一个数组,所以在ServerMetricsVO中会有一个List来对应此数组,于是我们再创建一个ServerMetricsItemVO,在ServerMetricsVO中有一个属性是List<ServerMetricsItemVO>. 为了体现Java的抽象编程,我们还创建了一个AbstractVO.最后创建一个Junit Class 来进行测试。
一、AbtractVO
public abstract class AbstractVO {
/**
* Wrap the JSON format string to related VO object
* @param jsonString
* @return
*/
public abstract AbstractVO fromGson(String jsonString);
}
二、ServerMetricsVO
public class ServerMetricsVO extends AbstractVO {
/**
* The property name must equal the property key name in returned JSON format
*/
private List<ServerMetricsItemVO> serverMetrics;
public ServerMetricsVO fromGson(String jsonString) {
// Gson gson = new Gson(); // it will cause some Date parsed exception in some environment.
Gson gson = new GsonBuilder().setDateFormat(DateUtils.MIDDLE_LINE_TIMESTAMP).create();
ServerMetricsVO serverMetricsVO = gson.fromJson(jsonString, ServerMetricsVO.class);
return serverMetricsVO;
}
public List<ServerMetricsItemVO> getServerMetrics() {
return serverMetrics;
}
public void setServerMetrics(List<ServerMetricsItemVO> serverMetrics) {
this.serverMetrics = serverMetrics;
}
}
三、ServerMetricsItemVO
/**
* This class is used to accept the JSON request and wrapped it to
* the class
* @author Josh Wang(Sheng)
*
*/
public class ServerMetricsItemVO {
private String vip;
private String metricsName;
private String hostName;
private Date metricsTime;
private int partitionKey;
private double value;
private String thresholdParam1;
private String thresholdParam2;
private String thresholdParam3;
private String metricsP1;
private String metricsP2;
private String metricsP3;
/**
* table id used to decide to query source, will not persist into Database.
*/
private String tableId;
public String getVip() {
return vip;
}
public void setVip(String vip) {
this.vip = vip;
}
public String getMetricsName() {
return metricsName;
}
public void setMetricsName(String metricsName) {
this.metricsName = metricsName;
}
public String getHostName() {
return hostName;
}
public void setHostName(String hostName) {
this.hostName = hostName;
}
public Date getMetricsTime() {
return metricsTime;
}
public void setMetricsTime(Date metricsTime) {
this.metricsTime = metricsTime;
}
public int getPartitionKey() {
return partitionKey;
}
public void setPartitionKey(int partitionKey) {
this.partitionKey = partitionKey;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
public String getThresholdParam1() {
return thresholdParam1;
}
public void setThresholdParam1(String thresholdParam1) {
this.thresholdParam1 = thresholdParam1;
}
public String getThresholdParam2() {
return thresholdParam2;
}
public void setThresholdParam2(String thresholdParam2) {
this.thresholdParam2 = thresholdParam2;
}
public String getThresholdParam3() {
return thresholdParam3;
}
public void setThresholdParam3(String thresholdParam3) {
this.thresholdParam3 = thresholdParam3;
}
public String getMetricsP1() {
return metricsP1;
}
public void setMetricsP1(String metricsP1) {
this.metricsP1 = metricsP1;
}
public String getMetricsP2() {
return metricsP2;
}
public void setMetricsP2(String metricsP2) {
this.metricsP2 = metricsP2;
}
public String getMetricsP3() {
return metricsP3;
}
public void setMetricsP3(String metricsP3) {
this.metricsP3 = metricsP3;
}
public String getTableId() {
return tableId;
}
public void setTableId(String tableId) {
this.tableId = tableId;
}
}
四、TestVOConverterService
public class TestVOConverterService {
private static IVOConverterService converterService;
@BeforeClass
public static void setUp() {
converterService = (IVOConverterService)context.getBean("voConverterService");
}
@Test
public void convert() {
String jsonString = "{\"serverMetrics\":[{\"vip\":\"caty1.vip.ebay.com\", \"metricsName\":\"CPU Number\", \"hostName\":\"caty1\", \"metricsTime\":\"20130101 13:22:15\", \"partitionKey\":\"00\", \"value\":\"1324\", \"thresholdParam1\":\"param1\", \"thresholdParam2\":\"param2\", \"thresholdParam3\":\"param3\", \"metricsP1\":\"p1\", \"metricsP2\":\"p2\", \"metricsP3\":\"p3\"}," +
"{\"vip\":\"caty2.vip.ebay.com\", \"metricsName\":\"Memory\", \"hostName\":\"caty2\", \"metricsTime\":\"20130102 09:22:15\", \"partitionKey\":\"00\", \"value\":\"23456\", \"thresholdParam1\":\"param12\", \"thresholdParam2\":\"param22\", \"thresholdParam3\":\"param32\", \"metricsP1\":\"p12\", \"metricsP2\":\"p22\", \"metricsP3\":\"p32\"}," +
"{\"vip\":\"caty3.vip.ebay.com\", \"metricsName\":\"Storage\", \"hostName\":\"caty3\", \"metricsTime\":\"20130103 19:22:15\", \"partitionKey\":\"00\", \"value\":\"56789\", \"thresholdParam1\":param13, \"thresholdParam23\":\"param2\", \"thresholdParam33\":\"param3\", \"metricsP13\":\"p1\", \"metricsP23\":\"p2\", \"metricsP33\":\"p3\"}" +
"]}";
// String jsonString = "{\"serverMetrics\":[{\"vip\":\"caty1.vip.ebay.com\", \"metricsName\":\"CPU Number\", \"hostName\":\"caty1\", \"metricsTime\":\"2013-01-01 13:22:15\", \"partitionKey\":\"00\", \"value\":\"1324\", \"thresholdParam1\":\"param1\", \"thresholdParam2\":\"param2\", \"thresholdParam3\":\"param3\", \"metricsP1\":\"p1\", \"metricsP2\":\"p2\", \"metricsP3\":\"p3\"}]}";
AbstractVO vo = new ServerMetricsVO();
ServerMetricsVO metricsVO = (ServerMetricsVO)converterService.convert(vo, jsonString);
for (ServerMetricsItemVO item : metricsVO.getServerMetrics()) {
print(item.getVip() + " " + item.getHostName() + " " + item.getMetricsName() + " " + item.getPartitionKey());
}
}
}
从上面的代码可以看出,用Goole的Gson类来做这个操作,那可是相当的简单,只需要构造一个Gson对象,传入一个json字符串和想要转换到的目标Object类即可完成转换。当然转换的时候一定要注意传入的json字符串是有效的json字符串。