JSON-lib包是一个beans,collections,maps,java arrays,XML和JSON互相转换的包,可以将Java对象转成json格式的字符串,也可以将Java对象转换成xml格式的文档,同样可以将json字符串转换成Java对象或是将xml字符串转换成Java对象。
JSON-lib下载
JSON-lib包下载地址:https://repo1.maven.org/maven2/net/sf/json-lib/json-lib/2.4/json-lib-2.4-jdk15.jar
JSON-lib 依赖的包,且不得低于以下版本:
jakarta commons-lang 2.5
jakarta commons-beanutils 1.8.0
jakarta commons-collections 3.2.1
jakarta commons-logging 1.1.1
ezmorph 1.0.6
打包下载:json-lib-all.zip
JSON-lib安装
1. Idea 中导入json包的方法。
ctrl+alt+shift+S 打开项目结构,依次点击Libraries->+加号。
将刚才下载的jar文件导入我们的项目
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
</dependency>
因为JSON-lib包依赖的包过多,建议使用maven安装的方法安装JSON-lib包
JSON-lib中Json字符串转对象
将Json字符串转成Java对象
import net.sf.json.JSONObject;
/**
*6. 将json字符串转成Java对象
*/
public class readJSON2Bean {
public static void main(String args[]) {
String json = "{\"address\":\"wuhan\",\"birthday\":\"2010-11-22\"," +
"\"email\":\"email@123.com\",\"id\":22,\"name\":\"yxjc123\"}";
JSONObject jsonObject = JSONObject.fromObject(json);
Student stu = (Student) JSONObject.toBean(jsonObject, Student.class);
System.out.println(stu.toString());
}
//这里方便设置为静态内部类
public static class Student{
@Override
public String toString() {
return "Student{" +
"address='" + address + '\'' +
", name='" + name + '\'' +
", email='" + email + '\'' +
", birthday='" + birthday + '\'' +
", id=" + id +
'}';
}
private String address;
private String name;
private String email;
private String birthday;
private Integer id;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
}
输出Student{address='wuhan', name='yxjc123', email='email@123.com', birthday='2010-11-22', id=22}
将Json字符串转成Java的List集合
import net.sf.json.JSONArray;
import java.util.List;
/**
*JSON字符串转成Java的List集合
*/
public class readJSON2List {
public static void main(String args[]) {
try {
String json = "{\"address\":\"wuhan\",\"birthday\":\"2010-11-22\"," +
"\"email\":\"email@123.com\",\"id\":22,\"name\":\"yxjc123\"}";
json = "[" + json + "]";
JSONArray jsonArray = JSONArray.fromObject(json);
List< Student > list = JSONArray.toList(jsonArray, Student.class);
System.out.println(list.size());
System.out.println(list.get(0));
list = JSONArray.toList(jsonArray);
System.out.println(list.size());
System.out.println(list.get(0));
} catch(Exception e) {
e.printStackTrace();
}
}
//这里方便设置为静态内部类
public static class Student{
private String address;
private String name;
private String email;
private String birthday;
private Integer id;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
}
输出:1
readJSON2List$Student@4926097b
1
net.sf.ezmorph.bean.MorphDynaBean@59f95c5d[
{birthday=2010-11-22, address=wuhan, name=yxjc123, id=22, email=email@123.com}
]
readJSON2List$Student@4926097b
1
net.sf.ezmorph.bean.MorphDynaBean@59f95c5d[
{birthday=2010-11-22, address=wuhan, name=yxjc123, id=22, email=email@123.com}
]
将Json字符串转换成动态Java对象
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import org.apache.commons.beanutils.PropertyUtils;
import java.lang.reflect.InvocationTargetException;
/**
*将json字符串转换成动态Java对象
*/
public class readJSON2DynaBean {
public static void main(String args[]) {
try {
String json = "{\"address\":\"wuhan\",\"birthday\":\"2010-11-22\"," +
"\"email\":\"email@123.com\",\"id\":22,\"name\":\"yxjc123\"}";
JSON jo = JSONSerializer.toJSON(json);
Object o = JSONSerializer.toJava(jo);
System.out.println(PropertyUtils.getProperty(o, "address").toString());
JSONObject jsonObject = JSONObject.fromObject(json);
System.out.println((jsonObject.getString("email")));
o = JSONSerializer.toJava(jsonObject);
System.out.println((PropertyUtils.getProperty(o, "name").toString()));
} catch(IllegalAccessException e) {
e.printStackTrace();
} catch(InvocationTargetException e) {
e.printStackTrace();
} catch(NoSuchMethodException e) {
e.printStackTrace();
}
}
//这里方便设置为静态内部类
public static class Student{
private String address;
private String name;
private String email;
private String birthday;
private Integer id;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
}
输出wuhan
email@123.com
yxjc123
email@123.com
yxjc123
将Json字符串转成xml对象
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;
/**
* Json转换Xml对象
*/
public class json2xml {
public static void main(String args[]) {
String json1 = "{'age':22,'sex':'男','userName':'yxjc123'}";
JSONObject jsonObj = JSONObject.fromObject(json1);
XMLSerializer x = new XMLSerializer();
String xml = x.write(jsonObj);
System.out.println("XML");
System.out.println(xml);
}
}
输出XML
22男yxjc123
JSON-lib中对象转Json字符串
Map集合转换成json字符串方法
import net.sf.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class MapToJSON {
public static void main(String[] args) {
Map map = new HashMap();
map.put("name", "json");
map.put("bool", Boolean.TRUE);
map.put("int", new Integer(1));
map.put("arr", new String[] { "a", "b" });
map.put("func", "function(i){ return this.arr[i]; }");
JSONObject json = JSONObject.fromObject(map);
System.out.println(json);
}
}
数组转换成json字符串方法
import net.sf.json.JSONArray;
public class BeanToJSON {
public static void main(String[] args) {
boolean[] boolArray = new boolean[] { true, false, true };
JSONArray jsonArray1 = JSONArray.fromObject(boolArray);
System.out.println(jsonArray1);
}
}
Java对象转换成json字符串方法
import net.sf.json.JSONArray;
import java.util.ArrayList;
import java.util.List;
/**
* 使用json-lib javabean转json
*/
public class BeanToJSON {
/**
* @param args
*/
public static void main(String[] args) {
List list = new ArrayList();
JsonBean2 jb1 = new JsonBean2();
jb1.setCol(1);
jb1.setRow(1);
jb1.setValue("xx");
JsonBean2 jb2 = new JsonBean2();
jb2.setCol(2);
jb2.setRow(2);
jb2.setValue("");
list.add(jb1);
list.add(jb2);
JSONArray ja = JSONArray.fromObject(list);
System.out.println(ja);
}
public static class JsonBean2{
private String value;
private Integer col;
private Integer row;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public Integer getCol() {
return col;
}
public void setCol(Integer col) {
this.col = col;
}
public Integer getRow() {
return row;
}
public void setRow(Integer row) {
this.row = row;
}
}
}
Collections转换Json字符串方法
import net.sf.json.JSONArray;
import java.util.ArrayList;
import java.util.List;
/**
* 集合转json
*/
public class CollectionToJSON {
/**
* main
*/
public static void main(String[] args) {
List list1 = new ArrayList();
list1.add("first");
list1.add("second");
JSONArray jsonArray1 = JSONArray.fromObject(list1);
System.out.println("List list1");
System.out.println(jsonArray1);
List < Student > list2 = new ArrayList < Student > ();
list2.add(new Student("stu1", "男", 10));
list2.add(new Student("stu2", "女", 11));
list2.add(new Student("stu3", "男", 12));
JSONArray jsonArray2 = JSONArray.fromObject(list2);
System.out.println("List<Student> list2");
System.out.println(jsonArray2);
}
public static class Student{
private String name;
private String sex;
private Integer age;
public Student(String name, String sex, int age) {
this.name = name;
this.age = age;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
}
Xml转换Json字符串方法
import net.sf.json.JSON;
import net.sf.json.xml.XMLSerializer;
public class XmlToJSON {
public static void main(String[] args) {
String s = "\n" +
" zs\n" +
" 男\n" +
" 20\n" +
" ";
XMLSerializer x = new XMLSerializer();
JSON json = x.read(s);
System.out.println("XmlToJson");
System.out.println(json.toString());
}
}
输出XmlToJson
{"name":{"@id":"n1","#text":"zs"},"sex":"男","age":"20"}
{"name":{"@id":"n1","#text":"zs"},"sex":"男","age":"20"}