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;
}
}