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)運(yùn)行截圖

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)運(yùn)行截圖

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)運(yùn)行截圖

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)運(yùn)行截圖

以上就是常用的幾種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使用POI實現(xiàn)Excel文件的創(chuàng)建與處理
這篇文章主要為大家詳細(xì)介紹了Java如何采用POI原生方式實現(xiàn)自定義Excel表格表頭并生成Excel文件,文中的示例代碼講解詳細(xì),有需要的小伙伴可以了解下2025-05-05
在Spring Boot中實現(xiàn)文件上傳與管理的操作
在 Spring Boot 中實現(xiàn)文件上傳與管理非常簡單,通過配置文件上傳、創(chuàng)建文件上傳、下載、列表和刪除接口,我們可以輕松地處理文件操作,結(jié)合前端頁面,可以提供一個完整的文件管理系統(tǒng),這篇文章主要介紹了在Spring Boot中實現(xiàn)文件上傳與管理,需要的朋友可以參考下2024-07-07
實例講解Java設(shè)計模式編程中如何運(yùn)用代理模式
這篇文章主要介紹了Java設(shè)計模式編程中如何運(yùn)用代理模式,文中舉了普通代理和強(qiáng)制代理的例子作為代理模式的擴(kuò)展內(nèi)容,需要的朋友可以參考下2016-02-02
Maven中Could not find artifact XXXX的錯誤解決
本文主要介紹了Maven中Could not find artifact XXXX的錯誤解決,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03

