<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib --> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils --> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.3</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-collections/commons-collections --> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang --> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient --> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <!-- https://mvnrepository.com/artifact/net.sf.ezmorph/ezmorph --> <dependency> <groupId>net.sf.ezmorph</groupId> <artifactId>ezmorph</artifactId> <version>1.0.6</version> </dependency>
1. Java對象轉換成json(既創建json)
關鍵類:JSONObject jsonObject = JSONObject.from(Object obj);
使用說明:只要將Java對象傳入方法,那麼便可以得到JSONObject,然後你便可以直接json.toString();輸出json
2. 字符串轉換成Java對象(既解析json)
關鍵類:JSONObject json = new JSONObject(String jsonString);
json.get(String key);
//根據key來獲取值。key對Java對象來說就是屬性名,value就是屬性值
使用說明:首先將json字符串傳入JSONObject對象的構造函數中,得到一個JSONObject對象,再通過該對象進行獲取相應
的數據,從而構造出一個Java對象。
3.Java集合轉換成Json集合
關鍵類:JSONArray jsonArray = JSONArray.fromObject(Object obj);
使用說明:將Java集合對象直接傳進JSONArray.fromObject()中,得到一個JSONArray集合,再直接使用JSONArray的toString()
方法,便可得到json集合
4、 Json集合轉換成Java集合
關鍵類: JSONArray jsonArray = JSONArray.fromObject(Object obj);
使用說明:將json字符串對象傳入,便可得到一個JSONArray對象,再調用JSONArray對象的toCollection(JSONArray jsonArray
, Class clss)方法,便可得到一個Java對象的集合。