java 和 json 對(duì)象間轉(zhuǎn)換
1. json-lib是一個(gè)java類(lèi)庫(kù),提供將Java對(duì)象,包括beans, maps, collections, java arrays and XML等轉(zhuǎn)換成JSON,或者反向轉(zhuǎn)換的功能。
2. json-lib 主頁(yè) : http://json-lib.sourceforge.net/
3.執(zhí)行環(huán)境
需要以下類(lèi)庫(kù)支持
commons-lang 2.5
commons-beanutils 1.8.0
commons-collections 3.2.1
commons-logging 1.1.1
ezmorph 1.0.6
4.功能示例
這里通過(guò)JUnit-Case例子給出代碼示例
package com.mai.json;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.ezmorph.Morpher;
import net.sf.ezmorph.MorpherRegistry;
import net.sf.ezmorph.bean.BeanMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.junit.Test;
public class JsonLibTest {
/*
* 普通類(lèi)型、List、Collection等都是用JSONArray解析
*
* Map、自定義類(lèi)型是用JSONObject解析
* 可以將Map理解成一個(gè)對(duì)象,里面的key/value對(duì)可以理解成對(duì)象的屬性/屬性值
* 即{key1:value1,key2,value2......}
*
* 1.JSONObject是一個(gè)name:values集合,通過(guò)它的get(key)方法取得的是key后對(duì)應(yīng)的value部分(字符串)
* 通過(guò)它的getJSONObject(key)可以取到一個(gè)JSONObject,--> 轉(zhuǎn)換成map,
* 通過(guò)它的getJSONArray(key) 可以取到一個(gè)JSONArray ,
*
*
*/
//一般數(shù)組轉(zhuǎn)換成JSON
@Test
public void testArrayToJSON(){
boolean[] boolArray = new boolean[]{true,false,true};
JSONArray jsonArray = JSONArray.fromObject( boolArray );
System.out.println( jsonArray );
// prints [true,false,true]
}
//Collection對(duì)象轉(zhuǎn)換成JSON
@Test
public void testListToJSON(){
List list = new ArrayList();
list.add( "first" );
list.add( "second" );
JSONArray jsonArray = JSONArray.fromObject( list );
System.out.println( jsonArray );
// prints ["first","second"]
}
//字符串json轉(zhuǎn)換成json, 根據(jù)情況是用JSONArray或JSONObject
@Test
public void testJsonStrToJSON(){
JSONArray jsonArray = JSONArray.fromObject( "['json','is','easy']" );
System.out.println( jsonArray );
// prints ["json","is","easy"]
}
//Map轉(zhuǎn)換成json, 是用jsonObject
@Test
public void testMapToJSON(){
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 jsonObject = JSONObject.fromObject( map );
System.out.println( jsonObject );
}
//復(fù)合類(lèi)型bean轉(zhuǎn)成成json
@Test
public void testBeadToJSON(){
MyBean bean = new MyBean();
bean.setId("001");
bean.setName("銀行卡");
bean.setDate(new Date());
List cardNum = new ArrayList();
cardNum.add("農(nóng)行");
cardNum.add("工行");
cardNum.add("建行");
cardNum.add(new Person("test"));
bean.setCardNum(cardNum);
JSONObject jsonObject = JSONObject.fromObject(bean);
System.out.println(jsonObject);
}
//普通類(lèi)型的json轉(zhuǎn)換成對(duì)象
@Test
public void testJSONToObject() throws Exception{
String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";
JSONObject jsonObject = JSONObject.fromObject( json );
System.out.println(jsonObject);
Object bean = JSONObject.toBean( jsonObject );
assertEquals( jsonObject.get( "name" ), PropertyUtils.getProperty( bean, "name" ) );
assertEquals( jsonObject.get( "bool" ), PropertyUtils.getProperty( bean, "bool" ) );
assertEquals( jsonObject.get( "int" ), PropertyUtils.getProperty( bean, "int" ) );
assertEquals( jsonObject.get( "double" ), PropertyUtils.getProperty( bean, "double" ) );
assertEquals( jsonObject.get( "func" ), PropertyUtils.getProperty( bean, "func" ) );
System.out.println(PropertyUtils.getProperty(bean, "name"));
System.out.println(PropertyUtils.getProperty(bean, "bool"));
System.out.println(PropertyUtils.getProperty(bean, "int"));
System.out.println(PropertyUtils.getProperty(bean, "double"));
System.out.println(PropertyUtils.getProperty(bean, "func"));
System.out.println(PropertyUtils.getProperty(bean, "array"));
List arrayList = (List)JSONArray.toCollection(jsonObject.getJSONArray("array"));
for(Object object : arrayList){
System.out.println(object);
}
}
//將json解析成復(fù)合類(lèi)型對(duì)象, 包含List
@Test
public void testJSONToBeanHavaList(){
String json = "{list:[{name:'test1'},{name:'test2'}],map:{test1:{name:'test1'},test2:{name:'test2'}}}";
// String json = "{list:[{name:'test1'},{name:'test2'}]}";
Map classMap = new HashMap();
classMap.put("list", Person.class);
MyBeanWithPerson diyBean = (MyBeanWithPerson)JSONObject.toBean(JSONObject.fromObject(json),MyBeanWithPerson.class , classMap);
System.out.println(diyBean);
List list = diyBean.getList();
for(Object o : list){
if(o instanceof Person){
Person p = (Person)o;
System.out.println(p.getName());
}
}
}
//將json解析成復(fù)合類(lèi)型對(duì)象, 包含Map
@Test
public void testJSONToBeanHavaMap(){
//把Map看成一個(gè)對(duì)象
String json = "{list:[{name:'test1'},{name:'test2'}],map:{testOne:{name:'test1'},testTwo:{name:'test2'}}}";
Map classMap = new HashMap();
classMap.put("list", Person.class);
classMap.put("map", Map.class);
//使用暗示,直接將json解析為指定自定義對(duì)象,其中List完全解析,Map沒(méi)有完全解析
MyBeanWithPerson diyBean = (MyBeanWithPerson)JSONObject.toBean(JSONObject.fromObject(json),MyBeanWithPerson.class , classMap);
System.out.println(diyBean);
System.out.println("do the list release");
List<Person> list = diyBean.getList();
for(Person o : list){
Person p = (Person)o;
System.out.println(p.getName());
}
System.out.println("do the map release");
//先往注冊(cè)器中注冊(cè)變換器,需要用到ezmorph包中的類(lèi)
MorpherRegistry morpherRegistry = JSONUtils.getMorpherRegistry();
Morpher dynaMorpher = new BeanMorpher( Person.class, morpherRegistry);
morpherRegistry.registerMorpher( dynaMorpher );
Map map = diyBean.getMap();
/*這里的map沒(méi)進(jìn)行類(lèi)型暗示,故按默認(rèn)的,里面存的為net.sf.ezmorph.bean.MorphDynaBean類(lèi)型的對(duì)象*/
System.out.println(map);
/*輸出:
{testOne=net.sf.ezmorph.bean.MorphDynaBean@f73c1[
{name=test1}
], testTwo=net.sf.ezmorph.bean.MorphDynaBean@186c6b2[
{name=test2}
]}
*/
List<Person> output = new ArrayList();
for( Iterator i = map.values().iterator(); i.hasNext(); ){
//使用注冊(cè)器對(duì)指定DynaBean進(jìn)行對(duì)象變換
output.add( (Person)morpherRegistry.morph( Person.class, i.next() ) );
}
for(Person p : output){
System.out.println(p.getName());
/*輸出:
test1
test2
*/
}
}
}
5.下面提供上面例子所需的資源,包括jar包和代碼
/Files/mailingfeng/json-lib/json-lib用例所需jar包和java類(lèi).rar
相關(guān)文章
JavaWeb項(xiàng)目中springmvc和tomcat對(duì)靜態(tài)文件的處理
這篇文章主要介紹了JavaWeb項(xiàng)目中springmvc和tomcat對(duì)靜態(tài)文件的處理 的相關(guān)資料,需要的朋友可以參考下2016-07-07SpringMVC攔截器實(shí)現(xiàn)監(jiān)聽(tīng)session是否過(guò)期詳解
這篇文章主要介紹了SpringMVC攔截器實(shí)現(xiàn)監(jiān)聽(tīng)session是否過(guò)期詳解,還是比較不錯(cuò)的,這里分享給大家,供需要的朋友參考。2017-11-11MybatisPlus自帶的queryWrapper實(shí)現(xiàn)時(shí)間倒序方式
這篇文章主要介紹了MybatisPlus自帶的queryWrapper實(shí)現(xiàn)時(shí)間倒序方式,具有很好的參考價(jià)值,希望對(duì)的有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01Springboot靜態(tài)資源訪問(wèn)實(shí)現(xiàn)代碼解析
這篇文章主要介紹了Springboot靜態(tài)資源訪問(wèn)實(shí)現(xiàn)代碼解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06SpringBoot集成pf4j實(shí)現(xiàn)插件開(kāi)發(fā)功能的代碼示例
pf4j是一個(gè)插件框架,用于實(shí)現(xiàn)插件的動(dòng)態(tài)加載,支持的插件格式(zip、jar),本文給大家介紹了SpringBoot集成pf4j實(shí)現(xiàn)插件開(kāi)發(fā)功能的示例,文中通過(guò)代碼示例給大家講解的非常詳細(xì),需要的朋友可以參考下2024-07-07通過(guò)Java實(shí)現(xiàn)RSA加密與驗(yàn)證的方法詳解
RSA是一種非對(duì)稱(chēng)加密算法,是目前廣泛應(yīng)用于加密和數(shù)字簽名領(lǐng)域的一種加密算法,本文主要講述如何通過(guò)Java實(shí)現(xiàn)RSA加密與驗(yàn)證,應(yīng)用場(chǎng)景為與其他平臺(tái)對(duì)接接口時(shí),通過(guò)RSA加密和解密驗(yàn)證請(qǐng)求的有效性,在對(duì)接時(shí)雙方互換公鑰,需要的朋友可以參考下2023-12-12IntelliJ IDEA 2021 Tomcat 8啟動(dòng)亂碼問(wèn)題的解決步驟
很多朋友遇到過(guò)IntelliJ IDEA 2021 Tomcat 8啟動(dòng)的時(shí)候出現(xiàn)各種奇葩問(wèn)題,最近有童鞋反映IntelliJ IDEA 2021 Tomcat 8啟動(dòng)亂碼,正好我也遇到這個(gè)問(wèn)題,下面我把解決方法分享給大家需要的朋友參考下吧2021-06-06Java 中的HashMap詳解和使用示例_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java 中的HashMap詳解和使用示例_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理,需要的朋友可以參考下2017-05-05Mybatis-Plus集成Sharding-JDBC與Flyway實(shí)現(xiàn)多租戶(hù)分庫(kù)分表實(shí)戰(zhàn)
這篇文章主要為大家介紹了Mybatis-Plus集成Sharding-JDBC與Flyway實(shí)現(xiàn)多租戶(hù)分庫(kù)分表實(shí)戰(zhàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11