java中JSON与对象集合之间的转换所附jar包坐标

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值