java對(duì)象與json對(duì)象之間互相轉(zhuǎn)換實(shí)現(xiàn)方法示例
本文實(shí)例講述了java對(duì)象與json對(duì)象之間互相轉(zhuǎn)換實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class MainClass {
public static void main(String[] args) {
TestJsonBean();
TestJsonAttribute();
TestJsonArray();
}
@SuppressWarnings("rawtypes")
private static void TestJsonArray() {
Student student1 = new Student();
student1.setId(1);
student1.setName("jag");
student1.setSex("man");
student1.setAge(25);
student1.setHobby(new String[]{"籃球","游戲"});
Student student2 = new Student();
student2.setId(2);
student2.setName("tom");
student2.setSex("woman");
student2.setAge(23);
student2.setHobby(new String[]{"上網(wǎng)","跑步"});
List<Student> list = new ArrayList<Student>();
list.add(student1);
list.add(student2);
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray.toString());
JSONArray new_jsonArray=JSONArray.fromObject(jsonArray.toArray());
Collection java_collection=JSONArray.toCollection(new_jsonArray);
if(java_collection!=null && !java_collection.isEmpty())
{
Iterator it=java_collection.iterator();
while(it.hasNext())
{
JSONObject jsonObj=JSONObject.fromObject(it.next());
Student stu=(Student) JSONObject.toBean(jsonObj,Student.class);
System.out.println(stu.getName());
}
}
}
private static void TestJsonAttribute() {
/**
* 創(chuàng)建json對(duì)象并為該對(duì)象設(shè)置屬性
*/
JSONObject jsonObj = new JSONObject();
jsonObj.put("Int_att",25);//添加int型屬性
jsonObj.put("String_att","str");//添加string型屬性
jsonObj.put("Double_att",12.25);//添加double型屬性
jsonObj.put("Boolean_att",true);//添加boolean型屬性
//添加JSONObject型屬性
JSONObject jsonObjSon = new JSONObject();
jsonObjSon.put("id", 1);
jsonObjSon.put("name", "tom");
jsonObj.put("JSONObject_att",jsonObjSon);
//添加JSONArray型屬性
JSONArray jsonArray = new JSONArray();
jsonArray.add("array0");
jsonArray.add("array1");
jsonArray.add("array2");
jsonArray.add("array3");
jsonObj.put("JSONArray_att", jsonArray);
System.out.println(jsonObj.toString());
System.out.println("Int_att:"+jsonObj.getInt("Int_att"));
System.out.println("String_att:"+jsonObj.getString("String_att"));
System.out.println("Double_att:"+jsonObj.getDouble("Double_att"));
System.out.println("Boolean_att:"+jsonObj.getBoolean("Boolean_att"));
System.out.println("JSONObject_att:"+jsonObj.getJSONObject("JSONObject_att"));
System.out.println("JSONArray_att:"+jsonObj.getJSONArray("JSONArray_att"));
}
/**
* java對(duì)象與json對(duì)象互相轉(zhuǎn)換
*/
private static void TestJsonBean() {
/**
* 創(chuàng)建java對(duì)象
*/
Student student = new Student();
student.setId(1);
student.setName("jag");
student.setSex("man");
student.setAge(25);
student.setHobby(new String[]{"籃球","上網(wǎng)","跑步","游戲"});
/**
* java對(duì)象轉(zhuǎn)換成json對(duì)象,并獲取json對(duì)象屬性
*/
JSONObject jsonStu = JSONObject.fromObject(student);
System.out.println(jsonStu.toString());
System.out.println(jsonStu.getJSONArray("hobby"));
/**
* json對(duì)象轉(zhuǎn)換成java對(duì)象,并獲取java對(duì)象屬性
*/
Student stu = (Student) JSONObject.toBean(jsonStu, Student.class);
System.out.println(stu.getName());
/**
* 創(chuàng)建json對(duì)象
*/
JSONObject jsonObj = new JSONObject();
jsonObj.put("id",1);
jsonObj.put("name","張勇");
jsonObj.put("sex","男");
jsonObj.put("age",24);
//jsonObj.put("hobby",new String[]{"上網(wǎng)","游戲","跑步","音樂"});
//System.out.println(jsonObj.toString());
/**
* json對(duì)象轉(zhuǎn)換成java對(duì)象
*/
Student stud = (Student) JSONObject.toBean(jsonObj,Student.class);
System.out.println(stud.getName());
}
}
PS:關(guān)于json操作,這里再為大家推薦幾款比較實(shí)用的json在線工具供大家參考使用:
在線JSON代碼檢驗(yàn)、檢驗(yàn)、美化、格式化工具:
http://tools.jb51.net/code/json
JSON在線格式化工具:
http://tools.jb51.net/code/jsonformat
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
json代碼在線格式化/美化/壓縮/編輯/轉(zhuǎn)換工具:
http://tools.jb51.net/code/jsoncodeformat
在線json壓縮/轉(zhuǎn)義工具:
http://tools.jb51.net/code/json_yasuo_trans
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java操作json格式數(shù)據(jù)技巧總結(jié)》、《Java數(shù)組操作技巧總結(jié)》、《Java字符與字符串操作技巧總結(jié)》、《Java數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》及《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- JAVA對(duì)象JSON數(shù)據(jù)互相轉(zhuǎn)換的四種常見情況
- JSON的String字符串與Java的List列表對(duì)象的相互轉(zhuǎn)換
- 使用Jackson來實(shí)現(xiàn)Java對(duì)象與JSON的相互轉(zhuǎn)換的教程
- java 和 json 對(duì)象間轉(zhuǎn)換
- GSON實(shí)現(xiàn)Java對(duì)象與JSON格式對(duì)象相互轉(zhuǎn)換的完全教程
- JSON數(shù)據(jù)轉(zhuǎn)換成Java對(duì)象的方法
- Java中Json字符串直接轉(zhuǎn)換為對(duì)象的方法(包括多層List集合)
- json轉(zhuǎn)換成java對(duì)象示例
- 使用GSON庫將Java中的map鍵值對(duì)應(yīng)結(jié)構(gòu)對(duì)象轉(zhuǎn)換為JSON
- 使用GSON庫轉(zhuǎn)換Java對(duì)象為JSON對(duì)象的進(jìn)階實(shí)例詳解
- java對(duì)象與json對(duì)象間的相互轉(zhuǎn)換的方法
相關(guān)文章
Java中使用輾轉(zhuǎn)相除法求最大公約數(shù)
這篇文章主要介紹了Java中使用輾轉(zhuǎn)相除法求最大公約數(shù),本文直接給出代碼實(shí)例,需要的朋友可以參考下2015-05-05
Java多線程中不同條件下編寫生產(chǎn)消費(fèi)者模型方法介紹
這篇文章主要介紹了Java多線程中不同條件下編寫生產(chǎn)消費(fèi)者模型方法介紹,介紹了生產(chǎn)消費(fèi)者模型,然后分享了相關(guān)代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
java swing 創(chuàng)建一個(gè)簡單的QQ界面教程
這篇文章主要介紹了java swing 創(chuàng)建一個(gè)簡單的QQ界面教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Spring Boot 安全 API 構(gòu)建之加密解密功能的實(shí)踐記錄
本文詳述了如何在SpringBoot3.3環(huán)境中實(shí)施API加密的最佳實(shí)踐,包括選擇合適的加密算法,密鑰管理,數(shù)據(jù)加密,防止加密漏洞,安全日志記錄,測試和監(jiān)控等方面,同時(shí),文章也對(duì)RSA非對(duì)稱加密和AES對(duì)稱加密的實(shí)現(xiàn)步驟進(jìn)行了詳細(xì)的解析2024-10-10
Java發(fā)送form-data請(qǐng)求實(shí)現(xiàn)文件上傳
這篇文章主要為大家詳細(xì)介紹了Java發(fā)送form-data請(qǐng)求實(shí)現(xiàn)文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
SpringBoot輕松實(shí)現(xiàn)ip解析(含源碼)
IP地址一般以數(shù)字形式表示,如192.168.0.1,IP解析是將這個(gè)數(shù)字IP轉(zhuǎn)換為包含地區(qū)、城市、運(yùn)營商等信息的字符串形式,如“廣東省深圳市 電信”,這樣更方便人理解和使用,本文給大家介紹了SpringBoot如何輕松實(shí)現(xiàn)ip解析,需要的朋友可以參考下2023-10-10
SpringBoot實(shí)現(xiàn)國際化i18n詳解
國際化(Internationalization,簡稱i18n)是指在軟件應(yīng)用中支持多種語言和文化的能力,本文將介紹如何在Spring?Boot應(yīng)用中實(shí)現(xiàn)國際化,需要的可以參考下2024-12-12
利用JAVA反射,讀取數(shù)據(jù)庫表名,自動(dòng)生成對(duì)應(yīng)實(shí)體類的操作
這篇文章主要介紹了利用JAVA反射,讀取數(shù)據(jù)庫表名,自動(dòng)生成對(duì)應(yīng)實(shí)體類的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08

