<!-- 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对象的集合。