`
strong_fee
  • 浏览: 175873 次
  • 性别: Icon_minigender_1
  • 来自: 福州
社区版块
存档分类
最新评论

Json(Json-lib)

阅读更多

 

1、DownLoad

2、两种过滤属性的方法

3、使用Json-lib

4、Json与Date的相互转化

    默认情况下,将日期转化为Json时,讲得到所有有Getter的属性,但这通常不是我们想要的,需要实现JsonValueProcessor方法写一个Date对象的处理器。代码和使用方法在JSON与JAVA数据的转换中的“4.bean->JSON 日期转换”中。

    但是如何把上述带有特殊日期格式的Json转化为正确的对象呢,这里我们需要注册一个日期Morpher。使用方法如下:

 

public class Test {
	public static void main(String[] args) {
		String personData = "{\"birthday\":\"1980/01/01\",\"name\":\"testname\"}";
		JSONObject jsonPerson = JSONObject.fromObject(personData);
		String[] dateFormats = new String[] {"yyyy/MM/dd"};
		JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(dateFormats));
		Person person = (Person)JSONObject.toBean(jsonPerson, Person.class);
		System.out.println(person.getBirthday());
	}
}
 
关于Mopher的使用,参见
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics