Java tojson,Java中json-lib的各种数据转换,及对象转换
JSON 是目前流行的交换数据格式,经常有童鞋问我, json-lib 怎么操作 JSON , jackson 怎么操作 JSON , Goson 怎么操作 JSON 等等,本篇介绍 json-lib 的各种操作。
其他的 JSON  信息都在这里:https://www.sojson.com/tag_json.html 
json-lib 的jar包引入:https://www.sojson.com/blog/101
一、json-lib的JSON和JavaBean的相互转换。
1、自定义Java Bean 转JSON对象。
/**
 * JSON-Lib 对象转JSON
 */
@Test
public void entity2json() {
	//构建一个对象
	JSONModel model = new JSONModel();
	model.setAge(10);
	model.setName("张三");
	model.setDomain("https://www.sojson.com");
	
	//对象转JSON
	JSONObject json = JSONObject.fromObject(model);
	//转成String
	String jsonStr = json.toString();
	System.out.printf("json-lib,对象转JSON:%s",jsonStr);
	//json-lib,对象转JSON:{"age":10,"domain":"https://www.sojson.com","name":"张三"}
}这里是一个普通的自定义的 Java Bean 对象转换。
2、JSON字符串转成Java Bean 对象。
/**
 * JSON-Lib JSON转对象
 */
@Test
public void json2entity() {
	/*
	 * 刚刚的字符串
	 * {"age":10,"domain":"https://www.sojson.com","name":"张三"} 
	 * 但是我们要转义双引号,可以用网址  https://www.sojson.com/yasuo.html
	 */
	String jsonStr = "{\"age\":10,\"domain\":\"https://www.sojson.com\",\"name\":\"张三\"}";
	
	//JSON字符串转JSON对象
	JSONObject json = JSONObject.fromObject(jsonStr);
	//JSON对象 转 JSONModel对象
	JSONModel model = (JSONModel) JSONObject.toBean(json, JSONModel.class);
	
	//转成String 方便输出
	System.out.printf("json-lib,JSON转对象:%s",JSONObject.fromObject(model).toString());
	//json-lib,JSON转对象:{"age":10,"domain":"https://www.sojson.com","name":"张三"}
}二、json-lib的JSON和Map的相互转换。
1、Map转JSON。
/**
 * Map 转 JSON 
 */
@Test
public void map2json(){
	Map map = new HashMap();
	map.put("age",10 );
	map.put("name","张三");
	map.put("domain","https://www.sojson.com");
	//对象转JSON
	JSONObject json = JSONObject.fromObject(map);
	//转成String
	String jsonStr = json.toString();
	System.out.printf("json-lib,Map转JSON:%s",jsonStr);
	//json-lib,Map转JSON:{"age":10,"domain":"https://www.sojson.com","name":"张三"}
	
}  2、JSON转Map
/**
 * JSON-Lib JSON转Map
 */
@Test
public void json2map() {
	/*
	 * 刚刚的字符串
	 * {"age":10,"domain":"https://www.sojson.com","name":"张三"} 
	 * 但是我们要转义双引号,可以用网址  https://www.sojson.com/yasuo.html
	 */
	String jsonStr = "{\"age\":10,\"domain\":\"https://www.sojson.com\",\"name\":\"张三\"}";
	
	//JSON字符串转JSON对象
	JSONObject json = JSONObject.fromObject(jsonStr);
	//JSON对象 转 JSONModel对象
	Map model = (Map) JSONObject.toBean(json, Map.class);
	
	//转成String 方便输出
	System.out.printf("json-lib,JSON转Map:%s",JSONObject.fromObject(model).toString());
	//json-lib,JSON转Map:{"name":"张三","age":10,"domain":"https://www.sojson.com"}
}  三、json-lib的JSONArray和List的相互转换。
1.List转JSONArray。
/**
 * JSON-Lib List转JSONArray
 */
@Test
public void list2JSONArray() {
	//创建一个List
	List list = new ArrayList();
	JSONModel model = new JSONModel();
	model.setAge(10);
	model.setName("张三");
	model.setDomain("https://www.sojson.com");
	list.add(model);
	
	model = new JSONModel();
	model.setAge(25);
	model.setName("李四");
	model.setDomain("http://ping.sojson.com");
	list.add(model);
	
	//List 转 JSONArray
	JSONArray jsonArray = JSONArray.fromObject(list);
	//JSONArray 转 String
	String jsonArrayStr = jsonArray.toString();
	//输出
	System.out.printf("JSON-Lib List转JSONArray:%s",jsonArrayStr);
	//JSON-Lib List转JSONArray:
	/*
		[
		    {
		        "age": 10,
		        "domain": "https://www.sojson.com",
		        "name": "张三"
		    },
		    {
		        "age": 25,
		        "domain": "http://ping.sojson.com",
		        "name": "李四"
		    }
		]
	 */
}  2.JSONArray转List。
/**
 * JSON-Lib JSONArray转List
 */
@SuppressWarnings("unchecked")
@Test
public void jsonArray2List(){
	//刚刚的JSON字符串
	//[{"age":10,"domain":"https://www.sojson.com","name":"张三"},{"age":25,"domain":"http://ping.sojson.com","name":"李四"}]
	String jsonArrayStr = "[{\"age\":10,\"domain\":\"https://www.sojson.com\",\"name\":\"张三\"},{\"age\":25,\"domain\":\"http://ping.sojson.com\",\"name\":\"李四\"}]";
	
	//json字符串转成JSONArray
	JSONArray jsonArray = JSONArray.fromObject(jsonArrayStr);
	
	//JSONArray 转 List
	List list = JSONArray.toList(jsonArray, JSONModel.class);
	
	//输出
	System.out.printf("JSON-Lib JSONArray转List:\n%s",JSONArray.fromObject(list).toString());
	/*
	 JSON-Lib JSONArray转List:
	 [{"age":10,"domain":"https://www.sojson.com","name":"张三"},{"age":25,"domain":"http://ping.sojson.com","name":"李四"}]
	 */
} 这里有一个注意的地方,就是JSONArray.toList()这个方法是过期的。但是不影响使用。
 json-lib  介绍到这里就完毕了。然后下面有附件是json-lib 的Jar包。
版权所属:SO JSON在线解析
原文地址:https://www.sojson.com/blog/100.html
转载时必须以链接形式注明原始出处及本声明。
如果本文对你有帮助,那么请你赞助我,让我更有激情的写下去,帮助更多的人。
