后臺(tái)返回前端json list map object對(duì)象 javaBean 相互轉(zhuǎn)換 引入maven? 如下
? ?<dependency>
? ? ? ? ? <groupId>net.sf.json-lib</groupId>
? ? ? ? ? ? <artifactId>json-lib</artifactId>
? ? ? ? ? ? <version>2.4</version>
? ? ? ? ? ? <classifier>jdk15</classifier>
? ? ? ? </dependency>
public static void main(String[] args) {
// changListJson();
System.out.println("**********************************************");
// changMapJson();
// changBeanJson();
// getMapsToJSONArray();
getListAndToJSONArray();
System.out.println("**********************************************");
}
/**
* List集合轉(zhuǎn)換成json方法
*/
public static List changListJson() {
List list=new ArrayList();
list.add("銀川");
list.add("金分區(qū)");
JSONArray jsonArray=JSONArray.fromObject(list);
System.out.println(jsonArray);
return jsonArray;
}
/**
* Map 集合轉(zhuǎn)Json 方法
*/
public static Map changMapJson() {
Map map=new HashMap();
map.put("寧夏","銀川");
map.put("萬匯九州","金鳳區(qū)");
map.put("寧安大街","中苑大廈");
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);
return jsonObject;
}
/**
* Bean轉(zhuǎn)換成json代碼
*/
public static void changBeanJson() {
Product product=new Product();
product.setId("001");
product.setName("葉晨");
product.setAge("19");
? ? //? JSONObject json= JSONObject.fromObject(product);
// 將Bean轉(zhuǎn)換為JSONArray數(shù)據(jù)
? ? JSONArray ja3 = JSONArray.fromObject(product);
//
? ? Map map= new HashMap();
? ? map.put("name", "Edward");
? ? map.put("sex", "male");
? ? map.put("age", "24");
? ? List list = new ArrayList();
? ? list.add(map);
? ? // 將List轉(zhuǎn)換為JSONArray數(shù)據(jù)
? ? JSONArray ja2 = JSONArray.fromObject(list);
? ? // JSON格式數(shù)據(jù)解析對(duì)象
? ? JSONObject obj = new JSONObject();
? ? obj.put("js2", ja2);
? ? obj.put("product", ja3);
? ? System.out.println(obj);
}
/**
* Map轉(zhuǎn)換成JSONArray
*/
private static void getMapsToJSONArray() {
// map對(duì)象
Map<String, String> map = new HashMap<String, String>();
map.put("name", "john");
map.put("sex", "男");
map.put("age", "20");
Map<String, String> map1 = new HashMap<String, String>();
map1.put("name", "zhang");
map1.put("sex", "女");
map1.put("age", "21");
List<Map> list = new ArrayList<Map>();
list.add(map);
list.add(map1);
JSONArray object = JSONArray.fromObject(list);
System.out.println(object.toString());
}
/*
*? List與Student轉(zhuǎn)換成JSONArray
*/
private static void getListAndToJSONArray() {
// map對(duì)象
Map<String, String> map = new HashMap<String, String>();
map.put("name", "john");
map.put("sex", "男");
map.put("age", "20");
Map<String, String> map1 = new HashMap<String, String>();
map1.put("name", "zhang");
map1.put("sex", "女");
map1.put("age", "21");
List<Map> list = new ArrayList<Map>();
list.add(map);
list.add(map1);
// product對(duì)象
Product product = new Product("1", "男", "121");
// 將List轉(zhuǎn)換為JSONArray數(shù)據(jù)
? ? ? ? JSONArray jaList = JSONArray.fromObject(list);
? ? ? ? // 將Bean轉(zhuǎn)換為JSONArray數(shù)據(jù)
? ? ? ? JSONArray jaStudent = JSONArray.fromObject(product);
? // JSON格式數(shù)據(jù)解析對(duì)象
? ? ? ? JSONObject jsonObject = new JSONObject();
? ? ? ? //存入數(shù)據(jù)
? ? ? ? jsonObject.put("list", jaList);
? ? ? ? jsonObject.put("stu", jaStudent);
? ? ? ? //解析 通過Key==》獲得值
//? ? ? ? JSONArray array =jsonObject.getJSONArray("list");
System.out.println(jsonObject.toString());
}