需求:
有一个已知字段名的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字符串。
相关推荐
1、生成Json数据 2、使用Gson解析Json数据(单个json对象、json数组和json对象内嵌json数组) // 这里有两篇好心人的博文,有详细的讲解 http://www.jb51.net/article/32547.htm ...
Android Studio下使用Google Gson解析Json数据示例
这里进行介绍Gson解析复杂Json和简单Json的数据方式
Eclipse下使用Google Gson解析Json数据示例,挺好用的,亲测
google-gson-2.2.4.zip
Gson解析JSON数据的入门级源码
Gson解析复杂json数据,复杂的json数据用gson工具解析
使用gson解析json的一次测试使用
Google Gson解析Json数据应用实例
JSON、GSON解析json数据,详细介绍了json和gson的使用,解析复杂json数据
介绍:Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. 压缩包里有以下文件: ...
Gson解析(List和Map)格式json数据 - CSDN博客原创2013年11月23日 21:54:46Gson解析(List和Map)格式json数据天
google-json-gson-master,google的jsonjar包,提供json方法。
被大多数开发者所使用的java操作json的类库,成熟,简单。
Gson2.6.1解析Json数据工具包 Gson2.6.1解析Json数据工具包 Gson2.6.1解析Json数据工具包
gson的源+说明文档+源码
Eclipse下使用Google Gson解析Json数据示例
使用google 提供的gson.jar的包来生成json格式数据,并利用from json解析这些数据将这些数据显示在手机界面上。亲测可用,项目下载后能够直接运行。本资源是项目的原码哦!
Gson解析Json数据信息,生成Json格式的数据