java list转object_java中Array/List/Map/Object与Json互相转换详解

importjava.io.File;

importjava.io.FileNotFoundException;

importjava.io.FileReader;

importjava.io.IOException;

importjava.util.ArrayList;

importjava.util.LinkedHashMap;

importjava.util.List;

importjava.util.Map;

importnet.sf.ezmorph.bean.MorphDynaBean;

importnet.sf.json.JSONArray;

importnet.sf.json.JSONFunction;

importnet.sf.json.JSONObject;

publicclassJsonTest {

publicstaticvoidmain(String args[]) {

//javaArray和json互相转换

javaArrayAndJsonInterChange();

System.out.println("-------------------------------------");

//javaList和json互相转换

javaListAndJsonInterChange();

System.out.println("-------------------------------------");

//javaMpa和Json互转

javaMapAndJsonInterChange();

System.out.println("-------------------------------------");

//javaObject和jsonObject互转

javaObjectAndJsonInterChange();

}

/**

* javaArray和json互相转换

*/

publicstaticvoidjavaArrayAndJsonInterChange() {

// java 转数组

boolean[] boolArray =newboolean[] {true,false,true};

JSONArray jsonArray = JSONArray.fromObject(boolArray);

String s = jsonArray.toString();

System.out.println(s);

// 通过json获取数组中的数据

String result = readJson("configdata");

JSONArray jsonR = JSONArray.fromObject(result);

intsize = jsonR.size();

for(inti =0; i 

System.out.println(jsonR.get(i));

}

}

/**

* javaList和json互相转换

*/

publicstaticvoidjavaListAndJsonInterChange() {

List list = newArrayList();

list.add(newInteger(1));

list.add(newBoolean(true));

list.add(newCharacter('j'));

list.add(newchar[] {'j','s','o','n'});

list.add(null);

list.add("json");

list.add(newString[] {"json","-","lib"});

// list转JSONArray

JSONArray jsArr = JSONArray.fromObject(list);

System.out.println(jsArr.toString(4));

// 从JSON串到JSONArray

jsArr = JSONArray.fromObject(jsArr.toString());

// --从JSONArray里读取

// print: json

List   list=JSONArray.toList(JSONArray.fromObject(jsArr), new ComplaintFusionGb(), new JsonConfig());

System.out.println(((JSONArray) jsArr.get(6)).get(0));

}

/**

* javaMpa和Json互转

*/

publicstaticvoidjavaMapAndJsonInterChange() {

Map map = newLinkedHashMap();

map.put("integer",newInteger(1));

map.put("boolean",newBoolean(true));

map.put("char",newCharacter('j'));

map.put("charArr",newchar[] {'j','s','o','n'});

// 注:不能以null为键名,否则运行报net.sf.json.JSONException:

// java.lang.NullPointerException:

// JSON keys must not be null nor the 'null' string.

map.put("nullAttr",null);

map.put("str","json");

map.put("strArr",newString[] {"json","-","lib"});

map.put("jsonFunction",newJSONFunction(newString[] {"i"},"alert(i)"));

map.put("address",newAddress("P.O BOX 54534","Seattle, WA",42452,"561-832-3180","531-133-9098"));

// map转JSONArray

JSONObject jsObj = JSONObject.fromObject(map);

System.out.println(jsObj.toString(4));

// 从JSON串到JSONObject

jsObj = JSONObject.fromObject(jsObj.toString());

//第一种方式:从JSONObject里读取

// print: json

System.out.println(jsObj.get("str"));

// print: address.city = Seattle, WA

System.out.println("address.city = "+ ((JSONObject) jsObj.get("address")).get("city"));

//第二种方式:从动态Bean里读取数据,由于不能转换成具体的Bean,感觉没有多大用处

MorphDynaBean mdBean = (MorphDynaBean) JSONObject.toBean(jsObj);

// print: json

System.out.println(mdBean.get("str"));

//print: address.city = Seattle, WA

System.out.println("address.city = "+ ((MorphDynaBean) mdBean.get("address")).get("city"));

}

/**

* javaObject和jsonObject互转

*/

publicstaticvoidjavaObjectAndJsonInterChange(){

Address address=newAddress("P.O BOX 54534","Seattle, WA",42452,"561-832-3180","531-133-9098");

//object转JSONObject

JSONObject jsObj = JSONObject.fromObject(address);

System.out.println(jsObj.toString(4));

//JsonObject转java Object

Address addre***esult=(Address) JSONObject.toBean(jsObj, Address.class);

System.out.println("address.city = "+ addre***esult.getCity());

System.out.println("address.street="+addre***esult.getStreet());

System.out.println("address.tel = "+ addre***esult.getTel());

System.out.println("address.telTwo="+addre***esult.getTelTwo());

System.out.println("address.zip="+addre***esult.getZip());

}

/**

* 读取json文件

* @param fileName 文件名,不需要后缀

* @return

*/

publicstaticString readJson(String fileName) {

String result = null;

try{

File myFile = newFile("./config/"+ fileName +".json");

FileReader fr = newFileReader(myFile);

char[] contents =newchar[(int) myFile.length()];

fr.read(contents, 0, (int) myFile.length());

result = newString(contents);

fr.close();

} catch(FileNotFoundException e) {

e.printStackTrace();

} catch(IOException e) {

e.printStackTrace();

}

returnresult;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值