Java中String字符串转具体对象的几种常用方式
作者:爱北的琳儿
String对象可以用来存储任何字符串类型的数据,包括HTML、XML等格式的字符串,下面这篇文章主要给大家介绍了关于JavaString字符串转具体对象的几种常用方式,需要的朋友可以参考下
Java对象以User.class为例,注意:代码中使用到了lombok的@Data注解
package com.example.entity; import lombok.Data; @Data public class User { // 姓名 private String name; // 年龄 private int age; }
1.Jsonlib解析器
依赖
<!--json-lib--> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency>
字符串转对象-代码展示
package com.example.demo; import com.example.entity.User; import net.sf.json.JSONObject; public class TestA { public static void main(String[] args) throws Exception { String objectStr = "{\"name\":\"张三\",\"age\":\"18\"}"; //1、使用JSONObject JSONObject jsonObject = JSONObject.fromObject(objectStr); User user = (User) JSONObject.toBean(jsonObject, User.class); System.out.println("name:" + user.getName()); System.out.println("age:" + user.getAge()); } }
打印截图示例
延伸(对象转String)代码展示
package com.example.demo; import com.example.entity.User; import net.sf.json.JSONObject; public class TestA { public static void main(String[] args) throws Exception { User user = new User(); user.setName("张三"); user.setAge(18); // 1.对象转JSONObject JSONObject json = JSONObject.fromObject(user); // 2.JSONObject转String String strJson = json.toString(); System.out.println(strJson); } }
延伸(对象转String)运行截图
2.fastjson解析器
依赖
<!-- fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.75</version> </dependency>
字符串转对象-代码展示
package com.example.demo; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.example.entity.User; public class TestA { public static void main(String[] args) throws Exception { String objectStr = "{\"name\":\"张三\",\"age\":\"18\"}"; JSONObject jsonObject = JSON.parseObject(objectStr); User user = JSON.toJavaObject(jsonObject, User.class); System.out.println("name:" + user.getName()); System.out.println("age:" + user.getAge()); } }
打印截图示例
延伸(对象转String)代码展示
package com.example.demo; import com.alibaba.fastjson.JSON; import com.example.entity.User; public class TestA { public static void main(String[] args) throws Exception { User user = new User(); user.setName("张三"); user.setAge(18); String str = JSON.toJSONString(user); System.out.println(str); } }
延伸(对象转String)运行截图
3.jackson解析器
依赖
<!-- jackson --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> <version>2.7.1</version> </dependency>
字符串转对象-代码展示
package com.example.demo; import com.example.entity.User; import com.fasterxml.jackson.databind.ObjectMapper; public class TestA { public static void main(String[] args) throws Exception { String objectStr = "{\"name\":\"张三\",\"age\":\"18\"}"; ObjectMapper om = new ObjectMapper(); User user = om.readValue(objectStr, User.class); System.out.println("name:" + user.getName()); System.out.println("age:" + user.getAge()); } }
打印截图示例
延伸(对象转String)代码展示
package com.example.demo; import com.example.entity.User; import com.fasterxml.jackson.databind.ObjectMapper; public class TestA { public static void main(String[] args) throws Exception { User user = new User(); user.setName("张三"); user.setAge(18); ObjectMapper om = new ObjectMapper(); String str = om.writeValueAsString(user); System.out.println(str); } }
延伸(对象转String)运行截图
4.Gson解析器
依赖
<!-- 谷歌依赖 --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.6</version> </dependency>
字符串转对象-代码展示
package com.example.demo; import com.example.entity.User; import com.google.gson.Gson; public class TestA { public static void main(String[] args) throws Exception { String objectStr = "{\"name\":\"张三\",\"age\":\"18\"}"; Gson gson = new Gson(); User user = gson.fromJson(objectStr, User.class); System.out.println("name:" + user.getName()); System.out.println("age:" + user.getAge()); } }
打印截图示例
延伸(对象转String)代码展示
package com.example.demo; import com.example.entity.User; import com.google.gson.Gson; public class TestA { public static void main(String[] args) throws Exception { User user = new User(); user.setName("张三"); user.setAge(18); Gson gson = new Gson(); String str = gson.toJson(user); System.out.println(str); } }
延伸(对象转String)运行截图
以上就是常用的几种String转具体的java对象操作
总结
到此这篇关于Java中String字符串转具体对象的几种常用方式的文章就介绍到这了,更多相关Java String字符串转具体对象内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!