Java中String字符串轉(zhuǎn)具體對象的幾種常用方式
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>
字符串轉(zhuǎn)對象-代碼展示
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());
}
}打印截圖示例

延伸(對象轉(zhuǎn)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.對象轉(zhuǎn)JSONObject
JSONObject json = JSONObject.fromObject(user);
// 2.JSONObject轉(zhuǎn)String
String strJson = json.toString();
System.out.println(strJson);
}
}延伸(對象轉(zhuǎn)String)運行截圖

2.fastjson解析器
依賴
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
字符串轉(zhuǎn)對象-代碼展示
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());
}
}
打印截圖示例

延伸(對象轉(zhuǎn)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);
}
}延伸(對象轉(zhuǎn)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>
字符串轉(zhuǎn)對象-代碼展示
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());
}
}打印截圖示例

延伸(對象轉(zhuǎn)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);
}
}延伸(對象轉(zhuǎn)String)運行截圖

4.Gson解析器
依賴
<!-- 谷歌依賴 -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
字符串轉(zhuǎn)對象-代碼展示
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());
}
}打印截圖示例

延伸(對象轉(zhuǎn)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);
}
}延伸(對象轉(zhuǎn)String)運行截圖

以上就是常用的幾種String轉(zhuǎn)具體的java對象操作
總結(jié)
到此這篇關(guān)于Java中String字符串轉(zhuǎn)具體對象的幾種常用方式的文章就介紹到這了,更多相關(guān)Java String字符串轉(zhuǎn)具體對象內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java數(shù)據(jù)結(jié)構(gòu)之復(fù)雜度篇
算法復(fù)雜度分為時間復(fù)雜度和空間復(fù)雜度。其作用:?時間復(fù)雜度是度量算法執(zhí)行的時間長短;而空間復(fù)雜度是度量算法所需存儲空間的大小2022-01-01
Java 17 更新后的 strictfp 關(guān)鍵字
strictfp 可能是最沒有存在感的關(guān)鍵字了,很多人寫了多年 Java 甚至都不知道它的存在,strictfp,字面意思就是嚴格的浮點型。這玩意兒居然還有個關(guān)鍵字,可見其地位還是很高的。下面文章小編就帶大家詳細介紹其關(guān)鍵字,需要的朋友可以參考一下2021-09-09
Spark調(diào)優(yōu)多線程并行處理任務(wù)實現(xiàn)方式
這篇文章主要介紹了Spark調(diào)優(yōu)多線程并行處理任務(wù)實現(xiàn)方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-08-08
swagger添加權(quán)限驗證保證API(接口)安全性(兩種方法)
這篇文章主要介紹了swagger添加權(quán)限驗證保證API(接口)安全性(兩種方法),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
Java實現(xiàn)MySQL數(shù)據(jù)實時同步至Elasticsearch的方法詳解
MySQL擅長事務(wù)處理,而Elasticsearch(ES)則專注于搜索與分析,將MySQL數(shù)據(jù)實時同步到ES,可以充分發(fā)揮兩者的優(yōu)勢,下面我們就來看看如何使用Java實現(xiàn)這一功能吧2025-03-03

