java對象和json的來回轉(zhuǎn)換知識點(diǎn)總結(jié)
為了是java中的對象便于理解,我們可以使用一款比較好用的數(shù)據(jù)格式,在數(shù)據(jù)解析的時候也會經(jīng)常用到,它就是JSON。在這里我們轉(zhuǎn)換對象和字符串時,需要java先變成json對象的模式。為了防止有人對JSON數(shù)組和對象的概念混淆,我們會先對這兩個概念理解,然后帶來java對象和json的來回轉(zhuǎn)換的方法。
1.JSON數(shù)組和對象的區(qū)別
JSONArray是將數(shù)據(jù)轉(zhuǎn)換為數(shù)組形式:
strArray:[{“address”:”北京市西城區(qū)”,”age”:”23”,”name”:”JSON”}]
使用時需要用數(shù)組方式讀取json里面的數(shù)據(jù),strArray[0].address;
JSONObject是將數(shù)據(jù)轉(zhuǎn)換為對象形式:
strJson:{“address”:”北京市西城區(qū)”,”age”:”23”,”name”:”JSON”}
使用時直接使用對象方式讀取json里面的數(shù)據(jù),strArray.address;
2.對象轉(zhuǎn)換為JSON
先將java對象轉(zhuǎn)換為json對象,在將json對象轉(zhuǎn)換為json字符串
//1、使用JSONObject JSONObject json = JSONObject.fromObject(stu); //2、使用JSONArray JSONArray array=JSONArray.fromObject(stu); String strJson=json.toString(); String strArray=array.toString();
3.json字符串轉(zhuǎn)換為java對象
同樣先將json字符串轉(zhuǎn)換為json對象,再將json對象轉(zhuǎn)換為java對象,如下所示。
JSONObject obj = new JSONObject().fromObject(jsonStr);//將json字符串轉(zhuǎn)換為json對象
將json對象轉(zhuǎn)換為java對象
Person jb = (Person)JSONObject.toBean(obj,Person.class);//將建json對象轉(zhuǎn)換為Person對象
實(shí)例擴(kuò)展:
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對象并為該對象設(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對象與json對象互相轉(zhuǎn)換
*/
private static void TestJsonBean() {
/**
* 創(chuàng)建java對象
*/
Student student = new Student();
student.setId(1);
student.setName("jag");
student.setSex("man");
student.setAge(25);
student.setHobby(new String[]{"籃球","上網(wǎng)","跑步","游戲"});
/**
* java對象轉(zhuǎn)換成json對象,并獲取json對象屬性
*/
JSONObject jsonStu = JSONObject.fromObject(student);
System.out.println(jsonStu.toString());
System.out.println(jsonStu.getJSONArray("hobby"));
/**
* json對象轉(zhuǎn)換成java對象,并獲取java對象屬性
*/
Student stu = (Student) JSONObject.toBean(jsonStu, Student.class);
System.out.println(stu.getName());
/**
* 創(chuàng)建json對象
*/
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對象轉(zhuǎn)換成java對象
*/
Student stud = (Student) JSONObject.toBean(jsonObj,Student.class);
System.out.println(stud.getName());
}
}
到此這篇關(guān)于java對象和json的來回轉(zhuǎn)換知識點(diǎn)總結(jié)的文章就介紹到這了,更多相關(guān)java對象和json的來回轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VSCode新手教程之配置Java環(huán)境的詳細(xì)教程
這篇文章主要給大家介紹了關(guān)于VSCode新手教程之配置Java環(huán)境的詳細(xì)教程,工欲善其事必先利其器,想要工作順利我們先搭建好JAVA的開發(fā)環(huán)境,需要的朋友可以參考下2023-10-10
springboot~nexus項(xiàng)目打包要注意的地方示例代碼詳解
這篇文章主要介紹了springboot~nexus項(xiàng)目打包要注意的地方,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
10個Elasticsearch查詢的實(shí)用技巧分享
Elasticsearch是一個非常流行的搜索引擎,已經(jīng)成為了許多企業(yè)的首選解決方案。本文將向大家介紹10個實(shí)用的Elasticsearch查詢技巧,并配上對應(yīng)的代碼示例,希望對大家有所幫助2023-04-04
Java中將String轉(zhuǎn)換為char數(shù)組的三種方式
這篇文章主要介紹了三種將字符串轉(zhuǎn)換為字符數(shù)組的方法,分別是toCharArray()、charAt()和循環(huán)、getChars(),每種方法都有其適用場景和優(yōu)缺點(diǎn),需要的朋友可以參考下2024-12-12
Mac M1 Java 開發(fā)環(huán)境配置詳解
這篇文章主要介紹了Mac M1 Java 開發(fā)環(huán)境配置詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Java中的三種校驗(yàn)注解的使用(@Valid,@Validated和@PathVariable)
本文主要介紹了Java中的三種校驗(yàn)注解的使用(@Valid,@Validated和@PathVariable),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04

