1. 程式人生 > 程式設計 >java物件和json的來回轉換知識點總結

java物件和json的來回轉換知識點總結

為了是java中的物件便於理解,我們可以使用一款比較好用的資料格式,在資料解析的時候也會經常用到,它就是JSON。在這裡我們轉換物件和字串時,需要java先變成json物件的模式。為了防止有人對JSON陣列和物件的概念混淆,我們會先對這兩個概念理解,然後帶來java物件和json的來回轉換的方法。

1.JSON陣列和物件的區別

JSONArray是將資料轉換為陣列形式:

strArray:[{“address”:”北京市西城區”,”age”:”23”,”name”:”JSON”}]

使用時需要用陣列方式讀取json裡面的資料,strArray[0].address;

JSONObject是將資料轉換為物件形式:

strJson:{“address”:”北京市西城區”,”name”:”JSON”}

使用時直接使用物件方式讀取json裡面的資料,strArray.address;

2.物件轉換為JSON

先將java物件轉換為json物件,在將json物件轉換為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字串轉換為java物件

同樣先將json字串轉換為json物件,再將json物件轉換為java物件,如下所示。

JSONObject obj = new JSONObject().fromObject(jsonStr);//將json字串轉換為json物件

將json物件轉換為java物件

Person jb = (Person)JSONObject.toBean(obj,Person.class);//將建json物件轉換為Person物件

例項擴充套件:

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[]{"上網","跑步"});
  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() {
  /**
   * 建立json物件併為該物件設定屬性
   */
  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物件互相轉換
  */
 private static void TestJsonBean() {
  /**
   * 建立java物件
   */
  Student student = new Student();
  student.setId(1);
  student.setName("jag");
  student.setSex("man");
  student.setAge(25);
  student.setHobby(new String[]{"籃球","上網","跑步","遊戲"});
  /**
   * java物件轉換成json物件,並獲取json物件屬性
   */
  JSONObject jsonStu = JSONObject.fromObject(student);
  System.out.println(jsonStu.toString());
  System.out.println(jsonStu.getJSONArray("hobby"));
  /**
   * json物件轉換成java物件,並獲取java物件屬性
   */
  Student stu = (Student) JSONObject.toBean(jsonStu,Student.class);
  System.out.println(stu.getName());
  /**
   * 建立json物件
   */
  JSONObject jsonObj = new JSONObject();
  jsonObj.put("id",1);
  jsonObj.put("name","張勇");
  jsonObj.put("sex","男");
  jsonObj.put("age",24);
  //jsonObj.put("hobby",new String[]{"上網","遊戲","音樂"});
  //System.out.println(jsonObj.toString());
  /**
   * json物件轉換成java物件
   */
  Student stud = (Student) JSONObject.toBean(jsonObj,Student.class);
  System.out.println(stud.getName());
 }
}

到此這篇關於java物件和json的來回轉換知識點總結的文章就介紹到這了,更多相關java物件和json的來回轉換內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!