In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
这篇文章主要讲解了"Json与JavaBean怎么转换",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Json与JavaBean怎么转换"吧!
maven依赖:
com.alibaba fastjson 1.1.22
举例:
import java.util.ArrayList;import java.util.Date;import java.util.List;import com.alibaba.fastjson.JSON;/** * 如果对象的属性没有被赋值,那么将对象转换为json串时: * * 字符串类型的属性: 默认会忽略该属性 * Integer类型的属性: 默认会忽略该属性 * Date类型的属性: 默认会忽略该属性 * int类型的属性: 默认会被转换为0 */public class TestFastJson { public static void main(String[] args) { User obj1 = new User(); obj1.setAge(1); obj1.setName("jack"); User obj2 = new User(); obj2.setAge(2); obj2.setName("tom"); obj2.setTime(new Date()); ArrayList arrayObj = new ArrayList(); arrayObj.add(obj1); arrayObj.add(obj2); // 将对象转换为json串 String jsonString1 = JSON.toJSONString(obj1); // {"age":1,"name":"jack"} String jsonString2 = JSON.toJSONString(arrayObj); // [{"age":1,"name":"jack"},{"age":2,"name":"tom","time":1503289848086}] // 将json串转换为对象类型 User user = JSON.parseObject(jsonString1, User.class); List userList = JSON.parseArray(jsonString2, User.class); }}
maven依赖:
net.sf.json-lib json-lib 2.3 jdk15
举例:
import java.util.ArrayList;import java.util.Date;import java.util.List;import net.sf.json.JSONArray;import net.sf.json.JSONObject;import net.sf.json.JsonConfig;/** * 如果对象的属性没有被赋值,那么将对象转换为json串时: * * 字符串类型的属性: 默认会被转换为空串"" * int、Integer类型的属性: 默认会被转换为0 * Date类型的属性: 默认会被转换为null * */public class TestSfJson { public static void main(String[] args) { // 1.普通对象转换为json串 User obj1 = new User(); obj1.setName("jack"); obj1.setAge(17); JSONObject jsonObj1 = JSONObject.fromObject(obj1); String jsonStr1 = jsonObj1.toString(); System.out.println(jsonStr1); // {"age":17,"name":"jack"} // 2.设置Date类型属性值的处理策略 Employee obj2 = new Employee(); obj2.setName("jack"); obj2.setAge(17); obj2.setBirthday(new Date()); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor()); JSONObject jsonObj2 = JSONObject.fromObject(obj2,jsonConfig); String jsonStr2 = jsonObj2.toString(); System.out.println(jsonStr2); // {"age":17,"birthday":"2017-08-21 11:04:47","name":"jack"} // 3.数组对象转换为json串 User user1 = new User(); user1.setName("jack"); user1.setAge(17); User user2 = new User(); user2.setName("tom"); user2.setAge(18); List obj3 = new ArrayList(); obj3.add(user1); obj3.add(user2); // 或:User[] obj3 = {user1,user2}; JSONArray jsonObj3 = JSONArray.fromObject(obj3); String jsonStr3 = jsonObj3.toString(); System.out.println(jsonStr3); // [{"age":17,"name":"jack"},{"age":18,"name":"tom"}] // 4.直接构造JSONObject对象 JSONObject jsonObj = new JSONObject(); jsonObj.put("name", "jack"); jsonObj.put("age", 17); String jsonStr = jsonObj.toString(); System.out.println(jsonStr); // {"name":"jack","age":17} }}import java.text.SimpleDateFormat;import java.util.Date;import java.util.Locale;import net.sf.json.JsonConfig;import net.sf.json.processors.JsonValueProcessor;public class JsonDateValueProcessor implements JsonValueProcessor { private String format = "yyyy-MM-dd HH:mm:ss"; public JsonDateValueProcessor() { super(); } public JsonDateValueProcessor(String format) { super(); this.format = format; } [@Override](https://my.oschina.net/u/1162528) public Object processArrayValue(Object paramObject, JsonConfig paramJsonConfig) { return process(paramObject); } [@Override](https://my.oschina.net/u/1162528) public Object processObjectValue(String paramString, Object paramObject, JsonConfig paramJsonConfig) { return process(paramObject); } private Object process(Object value) { if (value instanceof Date) { SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA); return sdf.format(value); } return value == null ? "" : value.toString(); }}感谢各位的阅读,以上就是"Json与JavaBean怎么转换"的内容了,经过本文的学习后,相信大家对Json与JavaBean怎么转换这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.