后台返回前端json list map object对象 javaBean 相互转换 引入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集合转换成json方法
*/
public static List changListJson() {
List list=new ArrayList();
list.add("银川");
list.add("金分区");
JSONArray jsonArray=JSONArray.fromObject(list);
System.out.println(jsonArray);
return jsonArray;
}
/**
* Map 集合转Json 方法
*/
public static Map changMapJson() {
Map map=new HashMap();
map.put("宁夏","银川");
map.put("万汇九州","金凤区");
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转换成json代码
*/
public static void changBeanJson() {
Product product=new Product();
product.setId("001");
product.setName("叶晨");
product.setAge("19");
// JSONObject json= JSONObject.fromObject(product);
// 将Bean转换为JSONArray数据
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转换为JSONArray数据
JSONArray ja2 = JSONArray.fromObject(list);
// JSON格式数据解析对象
JSONObject obj = new JSONObject();
obj.put("js2", ja2);
obj.put("product", ja3);
System.out.println(obj);
}
/**
* Map转换成JSONArray
*/
private static void getMapsToJSONArray() {
// map对象
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转换成JSONArray
*/
private static void getListAndToJSONArray() {
// map对象
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对象
Product product = new Product("1", "男", "121");
// 将List转换为JSONArray数据
JSONArray jaList = JSONArray.fromObject(list);
// 将Bean转换为JSONArray数据
JSONArray jaStudent = JSONArray.fromObject(product);
// JSON格式数据解析对象
JSONObject jsonObject = new JSONObject();
//存入数据
jsonObject.put("list", jaList);
jsonObject.put("stu", jaStudent);
//解析 通过Key==》获得值
// JSONArray array =jsonObject.getJSONArray("list");
System.out.println(jsonObject.toString());
}