首先说明一下,json-lib相较于Jackson以及GSON,性能是最低的,并且长期没有更新,所以不推荐使用。
1.Maven Dependency
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.fool.json</groupId> <artifactId>json</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>JSON</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> </project>
先来看一个简单的DEMO
JsonlibDemo.java
package org.fool.json.jsonlib; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class JsonlibDemo { public static void main(String[] args) { String jsonContent = "{'hello':'world', 'hero':'heroine'}"; JSONObject jsonObject = JSONObject.fromObject(jsonContent); String str1 = jsonObject.getString("hello"); String str2 = jsonObject.getString("hero"); System.out.println(str1); System.out.println(str2); System.out.println("--------------------"); jsonContent = "[{'hello': 333, 'abc': false, 'xyz': {'a': 1, 'b': 'ab'}}, " + "{'hello': 555, 'abc': true, 'xyz': {'a': 3, 'b': 'ba'}}]"; JSONArray jsonArray = JSONArray.fromObject(jsonContent); for (Object object : jsonArray) { JSONObject jsonObject2 = JSONObject.fromObject(object); int value1 = jsonObject2.getInt("hello"); boolean value2 = jsonObject2.getBoolean("abc"); // String value33 = jsonObject2.getString("xyz"); JSONObject jsonObject3 = jsonObject2.getJSONObject("xyz"); int value3 = jsonObject3.getInt("a"); String value4 = jsonObject3.getString("b"); System.out.println(value1); System.out.println(value2); System.out.println(value3); System.out.println(value4); } } }
?
2.POJO
Person.java
package org.fool.json.jsonlib; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; public class Person { private String username; private String password; private int age; private String address; private List<String> list = new ArrayList<>(); public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } @Override public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); } }
?
3.JUnit Test
JsonlibTest.java
package org.fool.json.jsonlib; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.junit.Before; import org.junit.Test; public class JsonlibTest { private JSONObject jsonObject = null; private JSONArray jsonArray = null; private Person person = null; @Before public void setUp() throws Exception { person = new Person(); person.setUsername("zhangsan"); person.setPassword("123456"); person.setAddress("beijing"); person.setAge(30); person.getList().add("hello"); person.getList().add("world"); person.getList().add("Hero"); person.getList().add("Heroine"); } ...... }
?
testEntity2Json
... @Test public void testEntity2Json() { jsonObject = JSONObject.fromObject(person); System.out.println(jsonObject); } ...
?result
{"address":"beijing","age":30,"list":["hello","world","Hero","Heroine"],"password":"123456","username":"zhangsan"}
?
testArray2Json
... @Test public void testArray2Json() { Person[] persons = {person, person}; // int[] i = { 8, 8, 5, 6, 7 }; jsonArray = JSONArray.fromObject(persons); System.out.println(jsonArray); } ...
?result
[{"address":"beijing","age":30,"list":["hello","world","Hero","Heroine"],"password":"123456","username":"zhangsan"},{"address":"beijing","age":30,"list":["hello","world","Hero","Heroine"],"password":"123456","username":"zhangsan"}]
?
testList2Json
... @Test public void testList2Json() { List<Person> list = new ArrayList<>(); list.add(person); list.add(person); jsonArray = JSONArray.fromObject(list); System.out.println(jsonArray); } ...
?result
[{"address":"beijing","age":30,"list":["hello","world","Hero","Heroine"],"password":"123456","username":"zhangsan"},{"address":"beijing","age":30,"list":["hello","world","Hero","Heroine"],"password":"123456","username":"zhangsan"}]
?
testMap2Json
... @Test public void testMap2Json() { Map<String, Person> map = new HashMap<>(); map.put("001", person); map.put("002", person); jsonObject = JSONObject.fromObject(map); System.out.println(jsonObject); } ...
?result
{"001":{"address":"beijing","age":30,"list":["hello","world","Hero","Heroine"],"password":"123456","username":"zhangsan"},"002":{"address":"beijing","age":30,"list":["hello","world","Hero","Heroine"],"password":"123456","username":"zhangsan"}}
?
?
?