Json-lib使用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Json-lib使用相关的知识,希望对你有一定的参考价值。
- Json-lib
Json-lib 是一个 Java 类库(官网:http://json-lib.sourceforge.net/)可以实现如下功能:
- 转换 javabeans, maps, collections, java arrays 和 XML 成为 json 格式数据
- 转换 json 格式数据成为 javabeans 对象
Json-lib 需要的 jar 包
- commons-beanutils-1.8.3.jar
- commons-collections-3.2.1.jar
- commons-lang-2.6.jar
- commons-logging-1.1.1.jar
- ezmorph-1.0.6.jar
- json-lib-2.4-jdk15.jar
- Json-lib 的使用
1. 将 Array 解析成 Json 串。
@Test
public void arrToJson() {
/**
* 将Array转化成Json串
*/
String[] str = {"Json", "Json-lib", "Jackson", "Xml"};
JSONArray json = JSONArray.fromObject(str);
System.out.println(json);
/**
* 对象数组转为Json
*/
Person[] ps = { new Person(1, "略略", false, new Date(), Arrays.asList(new String[] {"打球", "游戏", "动漫"})),
new Person(2, "哈哈", false, new Date(), Arrays.asList(new String[] {"打球"}))
};
json = JSONArray.fromObject(ps);
System.out.println(json);
/**
* 将List集合转为Json
*/
List<Person> plist = new ArrayList<Person>();
plist.add(new Person(1, "略略", false, new Date(), Arrays.asList(new String[] {"打球", "游戏", "动漫"})));
plist.add(new Person(2, "哈哈", false, new Date(), Arrays.asList(new String[] {"打球"})));
json = JSONArray.fromObject(ps);
System.out.println(json);
}
运行结果如下:
["Json","Json-lib","Jackson","Xml"]
[{"birthday":{"date":14,"day":5,"hours":19,"minutes":29,"month":3,"seconds":56,"time":1492169396320,"timezoneOffset":-480,"year":117},"gender":false,"hobby":["打球","游戏","动漫"],"id":1,"name":"略略"},{"birthday":{"date":14,"day":5,"hours":19,"minutes":29,"month":3,"seconds":56,"time":1492169396320,"timezoneOffset":-480,"year":117},"gender":false,"hobby":["打球"],"id":2,"name":"哈哈"}]
[{"birthday":{"date":14,"day":5,"hours":19,"minutes":29,"month":3,"seconds":56,"time":1492169396320,"timezoneOffset":-480,"year":117},"gender":false,"hobby":["打球","游戏","动漫"],"id":1,"name":"略略"},{"birthday":{"date":14,"day":5,"hours":19,"minutes":29,"month":3,"seconds":56,"time":1492169396320,"timezoneOffset":-480,"year":117},"gender":false,"hobby":["打球"],"id":2,"name":"哈哈"}]
2. 将 JavaBean/Map 解析成 JSON 串。 使用JSONObject 解析:
@Test
public void beanToJson() {
/**
* 对象转化为Json
*/
Person p = new Person(1, "略略", false, new Date(), Arrays.asList(new String[] {"打球", "游戏", "动漫"}));
JSONObject json = JSONObject.fromObject(p);
System.out.println(json);
/**
* 解析map
*/
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "董事三");
map.put("age", 24);
json = JSONObject.fromObject(map);
System.out.println(json);
}
运行结果如下:
{"birthday":{"date":14,"day":5,"hours":19,"minutes":38,"month":3,"seconds":22,"time":1492169902753,"timezoneOffset":-480,"year":117},"gender":false,"hobby":["打球","游戏","动漫"],"id":1,"name":"略略"}
{"age":24,"name":"董事三"}
3. 使用 JsonConfig 过虑属性:适用于 JavaBean/Map
运行结果如下,在运行结果中我们可以看到 name 属性被过滤掉了:
{"gender":false,"hobby":["打球","游戏","动漫"],"id":1,"name":"略略"}
4. 将 Json 串转换成 Array:
@Test
public void jsonToArr() {
/**
* json转换成一般数组
*/
String[] str = {"Json", "Json-lib", "Jackson", "Xml"};
JSONArray json = JSONArray.fromObject(str);
Object strs = JSONArray.toArray(json);
System.out.println(strs);
System.out.println(Arrays.asList((Object[])strs));
List<Person> plist = new ArrayList<Person>();
plist.add(new Person(1, "略略", false, new Date(), Arrays.asList(new String[] {"打球", "游戏", "动漫"})));
plist.add(new Person(2, "哈哈", false, new Date(), Arrays.asList(new String[] {"打球"})));
json = JSONArray.fromObject(plist);
List<Person> ps = (List<Person>) JSONArray.toCollection(json, Person.class);
for (Person person : ps) {
System.out.println(person);
}
}
运行结果如下:
[Ljava.lang.Object;@39e87719
[Json, Json-lib, Jackson, Xml]
Person [id=1, name=略略, gender=false, birthday=Fri Apr 14 20:18:29 CST 2017, hobby=[打球, 游戏, 动漫]]
Person [id=2, name=哈哈, gender=false, birthday=Fri Apr 14 20:18:29 CST 2017, hobby=[打球]]
5. 将 Json 串转成 JavaBean:
@Test
public void jsonToBean() {
/**
* json转化为javabean
*/
Person p = new Person(1, "略略", false, new Date(), Arrays.asList(new String[] {"打球", "游戏", "动漫"}));
JSONObject json = JSONObject.fromObject(p);
Object o = JSONObject.toBean(json, Person.class);
System.out.println(o);
}
运行结果如下:
2017-4-14 20:19:51 net.sf.json.JSONObject toBean
信息: Property ‘day‘ of class java.util.Date has no write method. SKIPPED.
2017-4-14 20:19:51 net.sf.json.JSONObject toBean
信息: Property ‘timezoneOffset‘ of class java.util.Date has no write method. SKIPPED.
Person [id=1, name=略略, gender=false, birthday=Fri Apr 14 20:19:51 CST 2017, hobby=[打球, 游戏, 动漫]]
在将 Json 形式的字符串转换为 JavaBean 的时候需要注意 JavaBean 中必须有无参构造函数,否则会报如下找不到初始化方法的错误:
Exception in thread "main" net.sf.json.JSONException: java.lang.NoSuchMethodException: cn.sunzn.json.Person.<init>() at net.sf.json.JSONObject.toBean(JSONObject.java:288) at net.sf.json.JSONObject.toBean(JSONObject.java:233) at cn.sunzn.json.JsonLib.main(JsonLib.java:23) Caused by: java.lang.NoSuchMethodException: cn.sunzn.json.Person.<init>() at java.lang.Class.getConstructor0(Unknown Source) at java.lang.Class.getDeclaredConstructor(Unknown Source) at net.sf.json.util.NewBeanInstanceStrategy$DefaultNewBeanInstanceStrategy.newInstance(NewBeanInstanceStrategy.java:55) at net.sf.json.JSONObject.toBean(JSONObject.java:282) ... 2 more
以上是关于Json-lib使用的主要内容,如果未能解决你的问题,请参考以下文章