這篇文章主要介紹了使用Java怎么將Json字符串與Object對象進行轉(zhuǎn)換,創(chuàng)新互聯(lián)小編覺得不錯,現(xiàn)在分享給大家,也給大家做個參考,一起跟隨創(chuàng)新互聯(lián)小編來看看吧!
成都創(chuàng)新互聯(lián)公司是一家專注于成都網(wǎng)站設(shè)計、做網(wǎng)站、成都外貿(mào)網(wǎng)站建設(shè)公司與策劃設(shè)計,建寧網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:建寧等地區(qū)。建寧做網(wǎng)站價格咨詢:18980820575
public class User { private String name; private Integer age; private String location; public User() { } public User(String name) { this.name = name; } public User(String name, Integer age) { this.name = name; this.age = age; } public User(String name, Integer age, String location) { this.name = name; this.age = age; this.location = location; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", location='" + location + '\'' + '}'; } }
1、Json-Lib
maven依賴如下,需注意
net.sf.json-lib json-lib 2.4 jdk15
測試demo
import net.sf.json.JSONObject; public class JsonLibDemo { public static void main(String[] args) { //創(chuàng)建測試object User user = new User("李寧",24,"北京"); System.out.println(user); //轉(zhuǎn)成json字符串 JSONObject jsonObject = JSONObject.fromObject(user); String json = jsonObject.toString(); System.out.println(json); //json字符串轉(zhuǎn)成對象 JSONObject jsonObject1 = JSONObject.fromObject(json); User user1 = (User) JSONObject.toBean(jsonObject1,User.class); System.out.println(user1); } }
2、org.json
maven依賴如下
org.json json 20170516
測試demo
import org.json.JSONObject; public class OrgJsonDemo { public static void main(String[] args) { //創(chuàng)建測試object User user = new User("李寧",24,"北京"); System.out.println(user); //轉(zhuǎn)成json字符串 String json = new JSONObject(user).toString(); System.out.println(json); //json字符串轉(zhuǎn)成對象 JSONObject jsonObject = new JSONObject(json); String name = jsonObject.getString("name"); Integer age = jsonObject.getInt("age"); String location = jsonObject.getString("location"); User user1 = new User(name,age,location); System.out.println(user1); } }
3、Jackson
maven依賴
com.fasterxml.jackson.core jackson-databind 2.9.0
測試demo
import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonDemo { public static void main(String[] args) { //創(chuàng)建測試object User user = new User("李寧",24,"北京"); System.out.println(user); //轉(zhuǎn)成json字符串 ObjectMapper mapper = new ObjectMapper(); try { String json = mapper.writeValueAsString(user); System.out.println(json); //json字符串轉(zhuǎn)成對象 User user1 = mapper.readValue(json,User.class); System.out.println(user1); } catch (java.io.IOException e) { e.printStackTrace(); } } }
4、Gson
maven依賴
com.google.code.gson gson 2.8.1
測試demo
import com.google.gson.Gson; public class GsonDemo { public static void main(String[] args) { //創(chuàng)建測試object User user = new User("李寧",24,"北京"); System.out.println(user); //轉(zhuǎn)成json字符串 Gson gson = new Gson(); String json = gson.toJson(user); System.out.println(json); //json字符串轉(zhuǎn)成對象 User user1 = gson.fromJson(json,User.class); System.out.println(user1); } }
5、FastJson
maven依賴
com.alibaba fastjson 1.2.37
測試demo
import com.alibaba.fastjson.JSON; public class FastJsonDemo { public static void main(String[] args) { //創(chuàng)建測試object User user = new User("李寧",24,"北京"); System.out.println(user); //轉(zhuǎn)成json字符串 String json = JSON.toJSON(user).toString(); System.out.println(json); //json字符串轉(zhuǎn)成對象 User user1 = JSON.parseObject(json,User.class); System.out.println(user1); } }
json-lib時間有些久遠,jar包只更新到2010年
org.json用起來有些繁瑣
Jackson、Gson、FastJson只需一兩句話就可以搞定
以上就是創(chuàng)新互聯(lián)小編為大家收集整理的使用Java怎么將Json字符串與Object對象進行轉(zhuǎn)換,如何覺得創(chuàng)新互聯(lián)網(wǎng)站的內(nèi)容還不錯,歡迎將創(chuàng)新互聯(lián)網(wǎng)站推薦給身邊好友。