在IDEA中的springboot项目里写的一个测试例子,新建User类,四个字段,Long id , String password,String userName,int age; 以及带参构造,toString方法

在pom.xml中添加 net.sf.json-lib  依赖:

 <!-- 对象转json依赖包 -->
<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<!--不加下面这一项会报错,是因为json-lib支持版本是jdk1.5,所以要指定下版本-->
<classifier>jdk15</classifier>
</dependency>

json转换测试类:

 package com.lch.springboot04.jsonparse;

 import com.lch.springboot04.domain.User;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject; import java.util.*; /**
* 对象转换为json数据测试,通过net.sf.json-lib.jar
*/
public class JsonParse { public static void main(String[] args) {
/**
* object 转为json数组 :数组,对象,map,list,list<Map<String,Object>> 都可以
* object 转为json字符串:只有 自定义对象 和 map可以转换,其他都不能这样转;
*/ String[] arr = {"a", "b", "c"};
System.out.println("string数组转为json数组" + JSONArray.fromObject(arr)); //["a","b","c"]
// System.out.println("string数组转为json字符串"+ JSONObject.fromObject(arr).toString()); //无法转换:'object' is an array. Use JSONArray instead //java自定义对象转json
User jack = new User("123", "jack", 18);
System.out.println("Java对象转为json数组" + JSONArray.fromObject(jack));
//结果:[{"age":18,"id":0,"password":"123","userName":"jack"}]
System.out.println("Java对象转为json字符串" + JSONObject.fromObject(jack).toString());
//结果:{"age":18,"id":0,"password":"123","userName":"jack"} //list转json
User tom = new User("456", "tom", 20);
List<User> userList = new ArrayList<>();
userList.add(jack);
userList.add(tom);
System.out.println("list转为json数组" + JSONArray.fromObject(userList));
// 结果:list转为json数组[{"age":18,"id":0,"password":"123","userName":"jack"},{"age":20,"id":0,"password":"456","userName":"tom"}]
// System.out.println("list转为json字符串"+ JSONObject.fromObject(userList).toString()); //不能转为json字符串 // list<Map<String,Object>> 转json
List<Map<String, Object>> mapList = new ArrayList();
Map<String, Object> m1 = new HashMap<String, Object>();
m1.put("jack", jack);
m1.put("tom", tom);
Map<String, Object> m2 = new HashMap<String, Object>();
m2.put("alex", new User("789", "alex", 20));
mapList.add(m1);
mapList.add(m2);
System.out.println("map转json数组" + JSONArray.fromObject(m2));
// 转换结果:[{"alex":{"age":20,"id":0,"password":"789","userName":"alex"}}]
System.out.println("map转json字符串" + JSONObject.fromObject(m2));
//结果:{"alex":{"age":20,"id":0,"password":"789","userName":"alex"}}
System.out.println(" list<Map<String,Object>> 转json数组" + JSONArray.fromObject(mapList));
// 结果:[{"tom":{"age":20,"id":0,"password":"456","userName":"tom"},"jack":{"age":18,"id":0,"password":"123","userName":"jack"}},{"alex":{"age":20,"id":0,"password":"789","userName":"alex"}}]
// System.out.println(" list<Map<String,Object>> 转json字符串"+JSONObject.fromObject(mapList));//不能转,'object' is an array. Use JSONArray instead /**
* json字符串转map
*/
/* String jsonStr = "{\"alex\":{\"age\":20,\"id\":0,\"password\":\"789\",\"userName\":\"alex\"}}";
Map<String,Object> map = jsonToMap(JSONObject.fromObject(jsonStr).get("alex").toString());
System.out.println("json字符串转map:"+map.get("age"));*/
// {"total":2,"items":[{"age":15,"id":1,"name":"jack"},{"age":16,"id":2,"name":"tom"}]}
String jsonStr2 = "{\"total\":2,\"items\":[{\"age\":15,\"id\":1,\"userName\":\"jack\"},{\"age\":16,\"id\":2,\"userName\":\"tom\"}]}";
Map<String, Object> stringObjectMap = jsonToMap(jsonStr2);
for (Map.Entry<String, Object> entry : stringObjectMap.entrySet()) {
System.out.println(entry.getKey() + "--" + entry.getValue());
}
/**输出结果:
* total--2
items--[{"age":15,"id":1,"userName":"jack"},{"age":16,"id":2,"userName":"tom"}]
*/ // jsonstr转list,list中存放Java Bean
String jsonStr3 = JSONObject.fromObject(jsonStr2).get("items").toString();
System.out.println("jsonStr3=" + jsonStr3);
List list = jsonStr2ObjectList(jsonStr3, User.class);
for (Object u : list) {
System.out.println(u);
}
/**
* 输出User{id=1, password='null', userName='jack', age=15}
User{id=2, password='null', userName='tom', age=16}
*/ } /**
* json字符串转map
*
* @param jsonStr
* @return
*/
public static Map<String, Object> jsonToMap(String jsonStr) {
JSONObject jsonObject = JSONObject.fromObject(jsonStr); //首先转换为 JSONObject
Iterator<String> nameItr = jsonObject.keys();//获取jsonObject的keySet()的迭代器
String key;//接收key值
Map<String, Object> outMap = new HashMap<>(); //存放转换结果
while (nameItr.hasNext()) {
key = nameItr.next();
outMap.put(key, jsonObject.get(key));
}
return outMap;
} /**
* JSONObject类中keys()方法默认返回jsonObject的keyset的迭代器
* public Iterator keys() {
* this.verifyIsNull();
* return this.keySet().iterator();
* }
*/ /** json转list,list中存放JavaBean
* @param jsonStr 要转换的json字符串
* @param clazz javaBean的Class对象
* @return
*/
public static List jsonStr2ObjectList(String jsonStr, Class clazz) {
JSONArray array = JSONArray.fromObject(jsonStr);
List list = new ArrayList();
for (Iterator itr = array.iterator(); itr.hasNext(); ) {
JSONObject jsonObject = (JSONObject) itr.next();
//取出json字符串中的一个子字符串,转换为指定类型的JavaBean
Object bean = JSONObject.toBean(jsonObject, clazz);
list.add(bean);
}
return list;
}
}

结果:

java中对象转json,json转list,json转map-LMLPHP

java中对象转json,json转list,json转map-LMLPHP

05-14 00:08