主要分为以下四类:
一、json-lib
json-lib是较早出现且应用最广泛的json解析工具,但是较多的依赖第三方jar包。
使用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会产生缺陷,出现问题。因此json-lib如今一般无法满足互联网化的需求。
使用实例:
jar包及项目结构如下:
实体类Product.java
package cn.cf.domain;
/**
* 商品实体类
*
* @author Administrator
*
*/
public class Product {
private String name;
private Double price;
private String category;
private Double weight;
public Product() {
super();
}
public Product(String name, Double price, String category, Double weight) {
super();
this.name = name;
this.price = price;
this.category = category;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public Double getWeight() {
return weight;
}
public void setWeight(Double weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Product [name=" + name + ", price=" + price + ", category=" + category + ", weight=" + weight + "]";
}
}
测试代码TestDemo.java
- JavaBean/ list/ map转json
代码如下: